필터 지우기
필터 지우기

How do I wrap text using the disp or display function?

조회 수: 34 (최근 30일)
Anderson Scott
Anderson Scott 2015년 2월 16일
답변: Steven Lord 2023년 2월 17일
When writing a string using the disp or display function, sometimes the text runs off the page, which is unhelpful when publishing. I tried breaking the string and using an ellipsis (...), then reintroducing the string on the next line, though this leads to an unexpected MATLAB error. This is especially frustrating when the string is saved inside of a variable that I'm trying to display.
Example of problem: disp('abcdefghijklmnop' ...
'qrstuvwxyz')
How can I avoid this problem?

답변 (4개)

Roger Stafford
Roger Stafford 2015년 2월 16일
편집: Roger Stafford 2015년 2월 16일
I suggest using 'fprintf' instead of 'disp'. The "\n" symbol will give a new line.

Chad Greene
Chad Greene 2015년 9월 23일
I wrote a function for this called wraptext.

Karol Ondrejkovic
Karol Ondrejkovic 2023년 2월 17일
I found this solution in comments of Chad Green's wraptext function. Works well to me and I'm using it in livescript publishing.
Roberto Tron on 15 Aug 2018
The functionality can be easily and more efficiently implemented using string replacement with regular expression. E.g., to wrap text to 80 columns:
strWrapped=regexprep(str,'.{1,80}\s','$0\n');

Steven Lord
Steven Lord 2023년 2월 17일
You could use the textwrap function to wrap the text manually.
a = "The quick brown fox jumped over the lazy dog";
b = textwrap(a, 40)
b = 2×1 cell array
{'The quick brown fox jumped over the '} {'lazy dog' }
strlength(b{1})
ans = 36
If you're writing this in code, you can either concatenate the text together and use an ellipsis inside the square brackets:
c = ['The quick brown fox jumped over the ', ...
'lazy dog']
c = 'The quick brown fox jumped over the lazy dog'
Or if you're using a string array use + at the end of the first line followed by an ellipsis.
c = "The quick brown fox jumped over the " + ...
"lazy dog"
c = "The quick brown fox jumped over the lazy dog"

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by