Newlines and spaces in strings
조회 수: 5 (최근 30일)
이전 댓글 표시
Hello,
The following code creates a string, disp_string, to be displayed in a GUI text field:
% dist_f is a function that outputs an array of values to be printed
totals = dist_f(t,step);
for i = 1:length(totals),
numstr = sprintf('%.2f', totals(i), '\n');
disp_string = strcat(disp_string, numstr, ' ');
end
disp_string = strcat('Distances: ', disp_string);
% display disp_string on the GUI
set(handles.stats_text,'String',disp_string);
I would like what appears on the GUI to look like, for example,
Distances: 6.60
32.00
10.44
32.00
with the nice newlines after each distance, but instead it displays
Distances:6.6032.0010.4432.00
without any newlines or spaces anywhere. I've got the formatting all wrong, I think. What am I doing wrong / is this the wrong way to do this?
Thanks in advance for help!
댓글 수: 0
채택된 답변
Jan
2011년 8월 2일
Append the newline to the format string:
...
numstr = sprintf(' %.2f\n', totals(i));
...
I'm not sure, where you want to have the space.
SPRINTF can handle vectors also, such that you can omit the FOR loop:
totals = dist_f(t,step);
disp_string = ['Distances: ', sprintf(' %.2f\n', totals)];
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!