1

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.

vittal rao
  • 17
  • 1
  • 8
  • Could you link an image as to what the message box would look like if you printed a string that filled an area larger than that of the message box? For example, `msgbox( sprintf( '1\n2\n3\n4\n5... so on' ) )` – TypeKazt May 17 '16 at 17:26

1 Answers1

0

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'

il_raffa
  • 5,090
  • 129
  • 31
  • 36
  • Thanks man. Yes I have data that exceed the size of normal message box, so I need to create a figure with scroll bar. I tried your suggestion to create a figure and use append it with data. But my message box displays only the last sprintf statement. It does not really append as you show in your example. I am trying to figure out why. Could you think why it could be ? – vittal rao May 19 '16 at 12:23
  • I've tested the code I've posted and it works. Please, check if you have implmented it in the righe way: to "appned" the strings you have to add in the call to `sprintf`: a `%s` at the beginning and `str` as the first of the input (that is, before `luser`. Anyway, I've updated the code by inserting the whole string you want to display (a part from the values of the varaibles that have been replaced by some "hard coded" values). Please,let me know if works. – il_raffa May 19 '16 at 17:53
  • I had indeed missed the %s. I now get how it works. Thanks a lot. – vittal rao May 20 '16 at 08:17