Live editor text formatting

37 views (last 30 days)
David Cole
David Cole on 27 Oct 2024 at 15:43
Answered: 埃博拉酱 on 28 Oct 2024 at 14:03
I am using live editor and the fprintf command. I do not want a space between my outputs. I have not included \n but the output looks like double spacing. How can I get the second line to print directly below the first line without an addition line space?
Code
Output

Answers (2)

Hitesh
Hitesh on 27 Oct 2024 at 16:37
Hi David Cole,
The "fprintf" function does not inherently add spaces or newlines to your output unless specified. However, if it appears that there is an extra line space, it could be due to how the Live Editor formats the output.
You can get your output directly below the previous output without any additional line spaces by using following method:
  • Concatenate Strings: You can print multiple pieces of text, concatenate them into a single string before using "fprintf".
fprintf(['Transmission Line losses Bus 1-2: 11.4214 MW and 45.6857 MVAR\n', ...
'Transmission Line losses Bus 1-3: 1.8218 MW and 7.2872 MVAR']);
  • Use "disp" with "sprintf": You need to use "disp" with "sprintf" as it controls formatting better in the Live Editor.
disp(sprintf('Transmission Line losses Bus 1-2: 11.4214 MW and 45.6857 MVAR'));
disp(sprintf('Transmission Line losses Bus 1-3: 1.8218 MW and 7.2872 MVAR'));
  • Verify Live Editor Settings: Kindly ensure there are no specific settings in the Live Editor affecting the output format.

埃博拉酱
埃博拉酱 on 28 Oct 2024 at 14:03
When using fprintf in a live script, each line of output is independent. The output of your two calls will be placed in two text boxes, with no blank lines in between, just a certain distance to make it look as if there are blank lines.
If you need them to be in the same text box, you may:
  • Concatenate the strings with line breaks, as shown by @Hitesh
  • NOT use disp with sprintf. This makes no difference as I've tested.
  • Compress your fprintfs into one same line or for loop:

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!