如果您有很多行要显示在消息框中,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')
希望这会有所帮助。
卡普拉'