Solving MATLAB Questions: Quick Answer to Formatting Output

  • Context: MATLAB 
  • Thread starter Thread starter TheloniousMONK
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
SUMMARY

This discussion addresses formatting output in MATLAB, specifically how to display the sign of a number and customize the representation of imaginary numbers. To display a number with a sign, the '+g' flag can be used in the fprintf function, allowing for output like '+72' for positive numbers. Additionally, to format imaginary numbers with 'j' instead of 'i', users are encouraged to utilize the num2str function with appropriate formatting, although a more streamlined approach is not provided.

PREREQUISITES
  • Basic understanding of MATLAB syntax and functions
  • Familiarity with the fprintf function for formatted output
  • Knowledge of complex numbers in MATLAB
  • Experience with string manipulation functions like num2str
NEXT STEPS
  • Explore advanced formatting options in MATLAB using fprintf
  • Learn about custom formatting for complex numbers in MATLAB
  • Investigate MATLAB's string manipulation functions for better output control
  • Review MATLAB documentation on formatting output for additional flags and options
USEFUL FOR

MATLAB users, data analysts, and engineers looking to improve their output formatting skills in MATLAB, particularly those working with numerical and complex data types.

TheloniousMONK
Messages
16
Reaction score
0
I am pretty new to MATLAB and am having a problem formatting my output. I have two questions:

1. How do I display the sign of a number in front of it similar to showpos in C++? For example, if a = 72, I want to display '+72', but if a = -72, I do not want to display '+-72'.

2. Is there a way to make it make imaginary output use j instead of i without doing something like:

num2str( real(Comp1) , '%.3f' ) + num2str( imag(Comp1) , '%.3fj' )

Any help would be greatly appreciated.
 
Physics news on Phys.org
TheloniousMONK said:
I am pretty new to MATLAB and am having a problem formatting my output. I have two questions:

1. How do I display the sign of a number in front of it similar to showpos in C++? For example, if a = 72, I want to display '+72', but if a = -72, I do not want to display '+-72'.

2. Is there a way to make it make imaginary output use j instead of i without doing something like:

num2str( real(Comp1) , '%.3f' ) + num2str( imag(Comp1) , '%.3fj' )

Any help would be greatly appreciated.
To display the sign you may use the + flag in the format. For instance:
fprintf(fid,'%+g',a), will print +72 if a=72 and -72 if a=-72.
fid is the file identifier. It is 1 for the screen.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K