MATLAB Solving MATLAB Questions: Quick Answer to Formatting Output

  • Thread starter Thread starter TheloniousMONK
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
To format output in MATLAB, the use of the '+' flag in the `fprintf` function allows for displaying the sign of a number. For example, using `fprintf(fid,'%+g',a)` will show '+72' for positive values and '-72' for negative values, where `fid` is the file identifier (1 for the screen). Regarding the display of imaginary numbers, to use 'j' instead of 'i', the recommended approach is to concatenate the real and imaginary parts using `num2str` with specific formatting. However, the discussion suggests that there may not be a direct built-in function to change the default representation of imaginary numbers from 'i' to 'j' without manually formatting the output.
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
Views
2K
Replies
32
Views
4K
Replies
5
Views
3K
Replies
1
Views
4K
Replies
4
Views
2K
Replies
5
Views
1K
Replies
4
Views
3K
Replies
4
Views
1K
Back
Top