Search code examples
matlabmessageboxmatlab-guidematlab-uitable

Appending data to a message box inside a 'for' loop in MatLab


I have written a function that uses user input to look up for closet value in one of the columns of a cell array and returns its indices. I use this index to print all the column values at that index. This is then displayed to the user a message box.

 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

Now I repeat the process inside a 'for' loop for several files and each time it will pop out a new 'message box like' this: Message boxes What would be the easiest way to display all the data in a single message box ? I tried storing in a file and re-displaying it which messes with the message box titles. I just want all the data one below the other in a single message box. Any suggestions?

thanks for the help.


Solution

  • 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.

    enter image description here

    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')
    

    enter image description here

    Hope this helps.

    Qapla'