Newlines and spaces in strings
8 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Jan
on 2 Aug 2011
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)];
More Answers (0)
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!