【问题标题】:Appending data to a message box inside a 'for' loop in MatLab在 MatLab 中的“for”循环内将数据附加到消息框
【发布时间】:2016-05-17 16:00:12
【问题描述】:

我编写了一个函数,它使用用户输入在元胞数组的一列中查找 closet 值并返回其索引。我使用此索引打印该索引处的所有列值。然后向用户显示一个消息框。

 prompt2 = 'Enter the Luminance value in cd/m^2';
 linp = inputdlg (prompt2,'Input Value',1);
 luser=str2double(linp);

 for ab=1:num_files
     files=char(fileloc(ab));
     dev=dlmread(files);
     round_tmp=abs(dev(:,4)-luser);
     [val,ind]=min(round_tmp);
     display(ind);
     msgbox(sprintf(' Values at %0.3f cd/m^2 for \n %s are:- \n\n Voltage    %0.3f V\n Current den.   %0.3f mA/cm^2 \n Current    %0.3f mA \n Efficiency   %0.3f cd/A',luser,forlegend{ab},dev(ind,1),dev(ind,5),dev(ind,2),dev(ind,6)),'Resulting Values');
 end

现在我在一个“for”循环中对几个文件重复这个过程,每次它都会弹出一个新的“消息框”,如下所示: Message boxes 在单个消息框中显示所有数据的最简单方法是什么?我尝试存储在一个文件中并重新显示它,这与消息框标题混淆。我只想在一个消息框中将所有数据放在另一个下方。有什么建议吗?

感谢您的帮助。

【问题讨论】:

  • 如果您打印的字符串填充的区域大于消息框的区域,您能否链接一个图像以说明消息框的外观?例如,msgbox( sprintf( '1\n2\n3\n4\n5... so on' ) )

标签: matlab messagebox matlab-guide matlab-uitable


【解决方案1】:

如果您有很多行要显示在消息框中,msgbox 不是最佳解决方案,因为它没有滚动条,您只会看到部分行。

在这种情况下,一个简单的解决方案是通过以下方式构建您自己的消息框:

  • 创建一个figure
  • 添加uicontrol editbox
  • 添加 OK pushbutton 关闭图窗(通过按钮的`回调

然后在循环中你可以:

  • 在每次迭代时生成字符串
  • 将其附加到上一个
  • 将字符串设置为uicontrol editbox 的string 属性

您可以使用 guide 创建更复杂的 GUI。

此方案在以下代码中实现:

创建您的 onw 消息框

% Create the figure
f=figure('unit','normalized','name','MY-_MESSAGE-_BOX','numbertitle','off');
% Add an edit text box
my_msg_box=uicontrol('parent',f,'style','edit','max',3,'unit','normalized', ...
   'HorizontalAlignment','left','position',[.1 .1 .7 .7])
% Add the OK button and set its callback to close the figure
ok_bt=uicontrol('parent',f,'style','pushbutton','unit','normalized', ...
   'string','OK','position',[.85 .5 .1 .1],'callback','delete(f);clear f')

创建字符串并将其添加到编辑框

编辑以使 OP 注释后的代码更清晰

在以下示例中,在每次迭代期间,都会生成要显示的字符串(我已将变量 luser,dev(ind,1),dev(ind,5),dev(ind,2),dev(ind,6) 替换为“数字”以测试代码)

% Generate the string in the loop
str='';
for i=1:10
   % Replace the msgbox instruction with the code to simply print the
   % string
   % msgbox(sprintf(' Values at %0.3f cd/m^2 for \n %s are:- \n\n Voltage    %0.3f V\n Current den.   %0.3f mA/cm^2 \n Current    %0.3f mA \n Efficiency   %0.3f cd/A',luser,forlegend{ab},dev(ind,1),dev(ind,5),dev(ind,2),dev(ind,6)),'Resulting Values');
   str=sprintf('%s\n Values at %0.3f cd/m^2 for \n %s are:- \n\n Voltage    %0.3f V\n Current den.   %0.3f mA/cm^2 \n Current    %0.3f mA \n Efficiency   %0.3f cd/A', ...
      str,123,'abcdef',111,222,333,444);
%    str=sprintf('%s This is:\n   the STRING #%d\n\n',str,i)
   % Add the strong to the edit box
   set(my_msg_box,'string',str)
end

基本上,您必须将 msgbox 指令替换为简单的 sprintf 调用。

关于sprintf 调用的实现(恢复变量luser,dev(ind,1),dev(ind,5),dev(ind,2),dev(ind,6) 的一部分),您必须:

  • 添加%s 作为第一个格式描述符
  • 添加str作为第一个输入参数

这将允许“附加”字符串。

如果您只有几行要显示,您可以简单地在循环的每次迭代中生成字符串并将其附加到前一个迭代中,而无需调用 msgbox 函数。

在循环结束时,您可以调用msgbox 并为其提供“整个”字符串。

str='';
% Generate the string in a loop
for i=1:10
   str=sprintf('%sThis is:\n   the STRING #%d\n\n',str,i)
end
% Display the final string in the msgbox
msgbox(str,'Resulting Values')

希望这会有所帮助。

卡普拉'

【讨论】:

  • 谢谢伙计。是的,我有超过普通消息框大小的数据,所以我需要创建一个带有滚动条的图形。我尝试了您的建议来创建一个图形并将其与数据一起使用。但我的消息框只显示最后一个 sprintf 语句。正如您在示例中显示的那样,它并没有真正附加。我试图找出原因。你能想到为什么会这样吗?
  • 我已经测试了我发布的代码并且它可以工作。请检查您是否以正确的方式实现它:要“应用”您必须在调用sprintf 时添加的字符串:%s 在开头和str 作为输入的第一个(即是,在luser 之前。无论如何,我已经通过插入要显示的整个字符串来更新代码(变量值的一部分已被一些“硬编码”值替换)。请让我知道是否有效。
  • 我确实错过了 %s。我现在明白它是如何工作的。非常感谢。
猜你喜欢
  • 2019-05-06
  • 2019-08-07
  • 2016-07-24
  • 2012-08-02
  • 2018-07-20
  • 1970-01-01
  • 2017-10-21
  • 2019-07-12
  • 2020-12-29
相关资源
最近更新 更多