在 MATLAB GUI 用户控件中处理回车/换行

发布于 2024-10-18 07:56:59 字数 806 浏览 4 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心凉怎暖 2024-10-25 07:56:59

对于列表框,将字符串属性设置为单元格

set(myListboxHandle,'String',{'myFirstLine';'mySecondLine'})

如果要添加另一行,请调用

contents = get(myListboxHandle,'String');
set(myListboxHandle,[contents;{'another line'}])

对于 GUI 中的多行文本,否则,请使用 char(10) 而不是 \n , IE

set(someUiControlHandle,'String',sprintf('my first line%smy second line',char(10)))

For a listbox, set the string property to a cell

set(myListboxHandle,'String',{'myFirstLine';'mySecondLine'})

If you want to add another line, call

contents = get(myListboxHandle,'String');
set(myListboxHandle,[contents;{'another line'}])

For multiline text in GUIs otherwise, use char(10) instead of \n, i.e.

set(someUiControlHandle,'String',sprintf('my first line%smy second line',char(10)))
笛声青案梦长安 2024-10-25 07:56:59

使用列表框时,通常更容易将选项处理为 字符串单元格数组。因此,您可以按如下方式初始化列表框:

set(handles.MyListBox,'String',{'Option 1'});

然后您可以向列表框添加选项,如下所示:

newOption = 'Option 2';
oldOptions = get(handles.MyListBox,'String');
set(handles.MyListBox,'String',[oldOptions; {newOption}]);

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:

set(handles.MyListBox,'String',{'Option 1'});

And then you can add options to your list box like so:

newOption = 'Option 2';
oldOptions = get(handles.MyListBox,'String');
set(handles.MyListBox,'String',[oldOptions; {newOption}]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文