MATLAB Answers

How do you send your code to the next line while using the disp comand?

4 views (last 30 days)
Iswarya on 6 Feb 2015
Answered: Jason Moore on 6 Feb 2015
Hello! I am attempting to use the disp command to output a long sentence. How do I send my code to the next line, so that when I publish my code into a PDF, it doesn't run off the page.
I tried to use the ellipses, but this doesn't seem to work. I also split the sentence in half by using two separate disp commands, but this just makes the sentence look "choppy". I would really like a continuous sentence that naturally moves to the next line.
Here is an example of what my code looks like:
disp(['This is a really really really really really really really really really long sentence with' num2str(numbers) 'also.'])

Answers (1)

Jason Moore
Jason Moore on 6 Feb 2015
Based upon the formatting you want to do I would suggest using sprintf instead of disp with sprintf you can use format specifications to control how the test is coming out.
Example: sprintf('My text on line 1 \nMy text on line2') Results in:
My text on line 1
My text on line2
Also with sprintf there are many other format strings that can help with spacing
If you still want to stick with the disp command you can get the same behavior with the following command
disp(['My text on line 1' char(10) 'My text on line2'])
In this example I am constructing one large string with the ascii value of 10 for a carriage return in between the two sections of text.

Community Treasure Hunt

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

Start Hunting!