在 MATLAB GUI 用户控件中处理回车/换行
我正在开发一个 MATLAB 程序,以便进行一些图像处理工作,并且需要在我临时创建的 MATLAB GUI 用户界面中使用用户控件。
该用户控件是一个列表框,我想在其中插入一些文本。好吧,问题不在于我不能在那里放置文本,我可以使用此调用来做到这一点:
set(handles.mylistbox, 'String', 'MyStringToPrint');
好吧,问题是此调用不允许我在列表框中插入多行,而只是覆盖前一行。
我希望找到一种方法让我的代码在新行中插入新文本。这应该不难做到,而且也是一个简单的模式:
texttoprint = 'My text to add'
oldtext = get(handles.MyListBox, 'String') %Holding the previous text here
set(handles.MyListBox, 'String', [oldtext '\n' texttoprint]) %Setting (no line feed printed)
set(handles.MyListBox, 'String', [oldtext char(10) texttoprint]) %Setting (this fails too)
好吧,它不会引发任何错误,但是 \n
不起作用。 我没有任何新线路...但需要!!!!
我应该如何解决这个问题? 问题是我需要在此用户控件中打印文本,而不是在 MATLAB 命令行上打印文本(只需执行 sprintf()
就非常简单)。
该怎么办?谢谢
I have a MATLAB program I am developing in order to do some image processing stuff and need to use a user control into a MATLAB GUI
user interface I created ad-hoc.
This user control is a List Box
and there I would like to insert some text. Well the problem is not that I cannot put text there, I can do that using this call:
set(handles.mylistbox, 'String', 'MyStringToPrint');
Well the problem is that this call does not let me insert many lines in my list box but just overwrite the previous one.
I wish to find a way to let my code insert the new text in a new line. This should not be so difficult to do and it is also a simple pattern:
texttoprint = 'My text to add'
oldtext = get(handles.MyListBox, 'String') %Holding the previous text here
set(handles.MyListBox, 'String', [oldtext '\n' texttoprint]) %Setting (no line feed printed)
set(handles.MyListBox, 'String', [oldtext char(10) texttoprint]) %Setting (this fails too)
Well it is ok and it does not raise any error BUT, \n
DOES NOT WORK.
I do not have any new line... BUT NEED TO!!!!
How should I solve this?
The problem is that I need to print text in this user control, not on the MATLAB commandline (that is very simple just by doing sprintf()
).
What to do? Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于列表框,将字符串属性设置为单元格
如果要添加另一行,请调用
对于 GUI 中的多行文本,否则,请使用
char(10)
而不是\n
, IEFor a listbox, set the string property to a cell
If you want to add another line, call
For multiline text in GUIs otherwise, use
char(10)
instead of\n
, i.e.使用列表框时,通常更容易将选项处理为 字符串单元格数组。因此,您可以按如下方式初始化列表框:
然后您可以向列表框添加选项,如下所示:
When working with list boxes it's usually easier to deal with the options as a cell array of strings. So, you would initialize your list box as follows:
And then you can add options to your list box like so: