If you have lots of line to be displayed in the message box, the msgbox
seens not the best solution since it has not a scrollbar and you will see only a partial set of lines.
In this case an easy solution could be to build your own message box by:
- create a
figure
- add a
uicontrol editbox
- add a OK
pushbutton
to close the figure (through the pushbutton's `callback
then in the loop you can:
- generate the string on each iteration
- append it to the previous one
- set the string to the
string
property of the uicontrol editbox
You can create a more sophisticaded GUI with guide
.
This solution is implemented in the following code:
Creation of your onw message box
% 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')
Create the string and add it to the editbox
EDIT to make more clear the code following the comment of the OP
In the following example, during each iteration, the string you want to display is generated (I've replaced the varaibles luser,dev(ind,1),dev(ind,5),dev(ind,2),dev(ind,6)
with "numbers" in order to test the code)
% 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
Basically, you have to replace the msgbox
instruction with simply the sprintf
call.
With respect to your implementation of the sprintf
call (a part restoring the varaibles luser,dev(ind,1),dev(ind,5),dev(ind,2),dev(ind,6)
), you have to:
- add
%s
as a first format descriptor
- add
str
as first input parameter
this will allow "appending" the strings.

In case you have only few line to display, you can simply generate the string on each iteration of the loop and to append it to the previous one without calling the msgbox
function.
At end of the loop, you can call msgbox
and provide it with the "whole" string.
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')

Hope this helps.
Qapla'