Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Quick MATLAB Question

  1. Nov 19, 2006 #1
    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.
  2. jcsd
  3. Nov 26, 2006 #2


    User Avatar

    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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Quick MATLAB Question
  1. Quick matlab question (Replies: 5)

  2. Matlab quick question (Replies: 1)