How to insert newline into displayed text in GUI?
51 次查看(过去 30 天)
显示 更早的评论
I have a text box in my GUIDE-generated GUI in which I want to display the file that I just loaded (and read data from). I want it so that on the top line is just a file name, and on the next line it shows the path. I can't figure out how to insert a newline character into the string that I want to display. Somewhere on a forum site (perhaps this one) I saw someone suggest using the sprintf function like this:
string = sprintf([filename, '\n', pathname]);
set(handles.textbox,'String',string)
This WOULD work if only my filename and pathname did not have all sorts of special characters like underscores and backslashes. I tried escaping them by using the strrep function and replacing all underscores ---> '\_' and likewise any backslash ---> '\\'. Then I tried to do
string = sprintf([filename2, '\n', pathname2]);
where filename2 and pathname2 have been processed with strrep . But it fails to work! I continue to get an error when I try to run the GUI and display the loaded file into the textbox:
Warning: Escape sequence '_' is not valid. See 'help sprintf' for valid escape sequences.
Any suggestions? Why is sprintf still screwing up with the underscore if I escaped it?
0 个评论
回答(2 个)
Image Analyst
2014-11-21
string = sprintf('%s\n%s', filename2, pathname2);
set(handles.text1, 'String', string);
0 个评论
Jasper van Casteren
2017-5-15
You need to initialize the textbox with a cell of strings. like
uicontrol(parent,'Style','text','String',{'Ready'}, 'tag', 'btTXT', . . .
then you can add lines by simple
h = guihandles(hObject)
h.btTXT.String = [h.btTXT.String; 'START'];
h.btTXT.String = [h.btTXT.String; sprintf('whatever you like = %d', whatever)];
1 个评论
Bogdan Dzyubak
2017-6-9
Wow, super neat.
MyBox = uicontrol('style','text','FontSize',20);
set(MyBox,'Units','normalized','Position',[xpos,ypos,xsize,ysize]);
% set(MyBox,'String',['Get good ', ':-)']); % Single line
set(MyBox,'String',{'Get good, ', ':-)'}); % Double line!
Does not appear to be required, but someone else had commented that using set(MyBox,'Max',2) increases number of allowed lines from 1 to 2.
另请参阅
类别
在 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!