Solving MATLAB Questions: Quick Answer to Formatting Output

  • Context: MATLAB 
  • Thread starter Thread starter TheloniousMONK
  • Start date Start date
  • Tags Tags
    Matlab
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 2K views
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.