matlab display string in multiple lines
古いコメントを表示
For example I want to disp('shdujfhdkjshfkjsdhkjfhkjdshfkjhsdkjfhkjsdhkjfhkjdshkfhskdhfkshdkjfhskjfdhs') but I dont want it all in one line because it continues off the screen in the command box, how do I create multiple lines?
回答 (5 件)
Walter Roberson
2013 年 10 月 9 日
disp(sprintf('shdujfhdkjshfkjsdhkjfhkjdshfkjhsdkjfhkjsdhkjfhkjdshkfhskdhfkshdkjfhskjfdhs'))
now at each place you want it broken, insert the two-character sequence \n
disp(sprintf('shdujfhdkjshfkjs\ndhkjfhkjdshfkjhsdkjfhkjsdhk\njfhkjdshkfhskdhfkshdkjfhskjfdhs'))
1 件のコメント
Jan
2013 年 10 月 9 日
The linebreak in the 2nd example comes from the display in the forum. In the real code the line is continued and not distributed over several lines.
Simon
2013 年 10 月 9 日
strvcat({'shdujfhdkjshfkjsdhkjfhkjdshfkj', ...
'hsdkjfhkjsdhkjfhkjdshkfhskdhfk', ...
'shdkjfhskjfdhs'})
Jan
2013 年 10 月 9 日
disp(['shdujfhdkjshfkjsdhkjfhkjdshfkj', ...
'hsdkjfhkjsdhkjfhkjdshkfhskdhfk', ...
'shdkjfhskjfdhs'])
6 件のコメント
Adriana Velasquez
2019 年 3 月 31 日
編集済み: Adriana Velasquez
2019 年 3 月 31 日
Hello! I'm just learning Matlab and I need to display a text on my graph but it's very long so I want to show it in 3 lines. Could you please help me? I have this but is not working. I also tried \n but it doesn't work...
txt = (['y=' num2str(m,2) 'x' '+' num2str(c,2) ' SSE=' num2str(sse,2), ...
' RMSE=' num2str(rmse,2) ' R^2= ' num2str(rsquare,2) ' n=' num2str(numel(x),2), ...
' p=' num2str(p_val,2) ]);
text(0.2,0.9,txt,'Units','normalized')
Walter Roberson
2019 年 3 月 31 日
Make txt a cell array of character vectors with one entry per line of output.
Adriana Velasquez
2019 年 3 月 31 日
Thank you Walter! I'll try that : ) Thanks for the fast answer!
Adriana Velasquez
2019 年 4 月 1 日
Sorry Walter, I still can't make it work when I include variables (even though I convert them from number to string..I don't understand why. Now, every single thing is a new line. Could you please give me an example of what you mean by "character vectors with one entry per line"?
Thanks a lot!!
txt=char({'y=' num2str(m,2) 'x' '+' num2str(c,2),...
'SSE=' num2str(sse,2) 'RMSE=' num2str(rmse,2) ' R^2= ' num2str(rsquare,2), ...
'n=' num2str(numel(x),2) 'p=' num2str(p_val,2)})
text(0.1,0.9,txt,'Units','normalized')

Walter Roberson
2019 年 4 月 1 日
I suggest you learn to use sprintf(). But in the mean time, generally you would use something like
txt = {['y=', num2str(m,2), 'x', '+', num2str(c,2)], ...
['SSE=', num2str(sse,2) ' RMSE=', num2str(rmse,2) ' R^2=', num2str(rsquare,2)], ...
['n=', num2str(numel(x),2), ' p=', num2str(p_val,2)] };
Each group inside [] goes together to form one line, and the different groups will get placed on different lines.
Adriana Velasquez
2019 年 4 月 2 日
wow excellent! Thanks a lot Walter! that helps me inmensely in "in the mean time". I know, I have to learn a lot, I just wanted to save some time using Matlab for my stats instead for doing 150 regressions manually! Thanks!

pathakunta
2024 年 1 月 26 日
0 投票
now at each place you want it broken, insert the two-character sequence \n Disp(sprintf('shdujfhdkjshfkjs\ndh
1 件のコメント
Walter Roberson
2024 年 1 月 26 日
This is the solution I suggested 10 years ago.
Another way to do this is to use textwrap, particularly if you're planning to use this to put the text in a control in an app.
s = 'shdujfhdkjshfkjsdhkjfhkjdshfkjhsdkjfhkjsdhkjfhkjdshkfhskdhfkshdkjfhskjfdhs'
t = textwrap(string(s), 40)
If you need this to be a character vector instead of a cell array of character vectors, you could use strjoin.
s2 = strjoin(t, newline)
カテゴリ
ヘルプ センター および File Exchange で Data Distribution Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!