在MATLAB GUI中显示分析结果 [英] Displaying analytical results in a MATLAB GUI

查看:1027
本文介绍了在MATLAB GUI中显示分析结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:我有一个MATLAB GUI,如果我运行它,我希望得到的分析结果显示在我的GUI中,而不是命令窗口中.我尝试使用列表框显示结果,因为会自动为列表框创建滑动条,但是它不起作用.如何显示数据,也许使用静态文本框?

My problem is this: I have a MATLAB GUI and I want the analytical results I get if I run it to appear in my GUI and not the command window. I tried using a listbox to display the results because of the slider bars that are automatically created for the listbox, but it did not work. How can I display the data, perhaps using a static text box?

推荐答案

首先,您必须确保禁止任何显示到命令窗口的输出.为此,请确保使用 DISP 函数来显示数据,并避免使用 FPRINTF 函数将数据发送到标准输出(即命令窗口).

First, you will have to make sure you suppress any output to the command window. You can do this by making sure you end lines with a semicolon, avoid using the DISP function to display data, and avoid using the FPRINTF function to send data to the standard output (i.e. command window).

第二,确定要显示哪种结果".如果它是数值的向量或矩阵,则可能需要使用 Azim对另一个问题的回答,并假设您具有较新版本的MATLAB).如果只是几个数值,字符或字符串,那么我建议使用静态文本框.例如:

Second, determine what sort of "results" you are wanting to display. If it is a vector or matrix of numeric values, you may want to use a UITABLE object instead of a static text box (as suggested in Azim's answer to your other question, and assuming you have one of the newer versions of MATLAB). If it is just a couple of numeric values, characters, or strings, then I would suggest using a static text box. For example:

hList = uicontrol('Style','text','Position',[100 100 200 200]);
set(hList,'String',{'Line 1'; 'Line 2'});  % Displays 2 lines, one string each
vec = rand(3,1);  % Column array of 3 random values
set(hList,'String',num2str(vec));  % Displays 3 lines, one number per line

使用此选项,您可能最终会完成

With this option, you will probably end up doing a lot with string operations.

注意::对于静态文本框,如果您在其中输入的文本超出了其显示能力,则它们只会截断文本.他们不会自动创建滑块来查看整个文本.您要么必须使静态文本框更大,要么调整设置为较小的值,或者(使用更复杂的选项)创建自己的滑块,该滑块将调整文本框中显示的内容.

NOTE: With static text boxes, if you put more text in them than they are able to display, they will simply cut off the text. They do not automatically create sliders to view the whole piece of text. You will either have to make the static text box bigger, adjust the "FontSize" property of the static text box to a smaller value, or (the more complicated option) create your own slider which will adjust what is displayed in the text box.

更复杂的选项...

万一有兴趣的人,我想我会提供一些示例代码来制作一个静态文本框,并带有一个控制文本框中显示内容的滑块.有很多不同的方法可以执行此操作,从我能想到的最简单的实现(如下所述)到使用

In case anyone is interested, I thought I'd include some sample code for making a static text box with a slider that controls the contents displayed in the text box. There are quite a few different ways to do this, ranging from the simplest implementation I can think of (given below) to more complicated versions using nested functions and fancy OOP stuff.

首先,您必须将以下两个功能另存为m文件:

First, you will have to have the two following functions saved as m-files:

callback_scrolltext.m

function callback_scrolltext(source,event,hText)
  textString = get(hText,'UserData');
  nLines = numel(textString);
  lineIndex = nLines-round(get(source,'Value'));
  set(hText,'String',textString(lineIndex:nLines));
end

update_scrolltext.m

function update_scrolltext(newText,hText,hSlider)
  newText = textwrap(hText,newText);
  set(hText,'String',newText,'UserData',newText);
  nRows = numel(newText);
  if (nRows < 2),
    sliderEnable = 'off';
  else
    sliderEnable = 'on';
  end
  nRows = max(nRows-1,1);
  set(hSlider,'Enable',sliderEnable,'Max',nRows,...
      'SliderStep',[1 3]./nRows,'Value',nRows);
end

第二,使用以下代码创建GUI对象.您可以将"Position"属性设置为所需的任意值,也可以使用图形或面板对象的任何句柄代替 hParent :

Second, create the GUI objects with the following code. You can set the 'Position' properties to whatever you want, as well as use any handle for a figure or panel object in place of hParent:

hParent = figure;
hText = uicontrol('Style','text',...
                  'Parent',hParent,...
                  'Units','pixels',...
                  'Position',[100 100 100 40]);
hSlider = uicontrol('Style','slider',...
                    'Parent',hParent,...
                    'Units','pixels',...
                    'Position',[200 100 10 40],...
                    'Enable','off',...
                    'Callback',{@callback_scrolltext,hText});

最后,每次您想更改文本框的文本时,请使用要显示的文本调用 update_scrolltext (包含在单元格数组中,就像输入 TEXTWRAP 以及文本框和滑块的句柄.以下是一些示例:

Finally, every time you want to change the text of the text box, call update_scrolltext with the text you want to display (contained in a cell array just like the string input to TEXTWRAP is) and the handles of the text box and slider. Here are some examples to try:

update_scrolltext({'hello'},hText,hSlider);
update_scrolltext({'hello'; 'there'; 'silly'; 'world'},hText,hSlider);

根据自己的喜好修改代码! =)

Modify the code as you see fit and enjoy! =)

这篇关于在MATLAB GUI中显示分析结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆