Solving MATLAB Questions: Quick Answer to Formatting Output

  • MATLAB
  • Thread starter TheloniousMONK
  • Start date
  • Tags
    Matlab
In summary: You can use the '%.3fj' format to display imaginary output with j instead of i. This will first convert the real part to a string using '%.3f', then concatenate it with the imaginary part, which is converted to a string using '%.3fj'. This will result in a string with the format 'a+bi', where a and b are the real and imaginary parts respectively.
  • #1
TheloniousMONK
16
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
  • #2
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.
 
  • #3


1. To display the sign of a number in front of it, you can use the fprintf function in MATLAB. For example, if you have a variable a that contains your number, you can use the following code to display it with the sign in front:

fprintf('%+d', a)

The '%+d' format specifier will display the sign in front of the number, and the 'd' indicates that it is an integer. You can also use '%+f' if your number is a floating-point value.

2. To make imaginary output use 'j' instead of 'i', you can change the default imaginary unit in MATLAB. You can do this by going to the Home tab, clicking on Preferences, and then selecting MATLAB > General > Display. From there, you can change the default imaginary unit from 'i' to 'j'. This will apply to all your output, so you don't need to use num2str to format it separately.
 

Related to Solving MATLAB Questions: Quick Answer to Formatting Output

1. How do I display my output in a specific format in MATLAB?

To format your output in MATLAB, you can use the fprintf function. This allows you to specify the format of your output, including the number of decimal places, spacing, and alignment.

2. Can I save my formatted output as a file in MATLAB?

Yes, you can save your formatted output as a file using the fprintf function. Simply specify the file name and path as the first argument, followed by the format and the data you want to save.

3. How do I include text or labels in my output in MATLAB?

To include text or labels in your output, you can use the fprintf function with the appropriate formatting. For example, to add a label for a value, you can use fprintf('Label: %d', value) where %d indicates the value will be an integer.

4. Can I format my output using variables in MATLAB?

Yes, you can use variables in your formatting by including them in the fprintf function. For example, fprintf('Value: %.2f', myVariable) will print the value of myVariable with 2 decimal places.

5. How do I display multiple values in a single line in MATLAB?

You can display multiple values in a single line by separating them with a comma in the fprintf function. For example, fprintf('Values: %d, %d, %d', value1, value2, value3) will print three values on the same line.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
969
Back
Top