tags:

views:

2634

answers:

4

My problem is this: I have a MATLAB GUI and I want the analytical results I get if I run it to appear in my GUI and not the command window. I tried using a listbox to display the results because of the slider bars that are automatically created for the listbox, but it did not work. How can I display the data, perhaps using a static text box?

+1  A: 

I usually just use a figure and text into it, e.g.

figure(gcf)
text(offsetX1, offsetX1, ['result 1: ' num2str(result1)])
text(offsetX2, offsetX2, ['result 2: ' num2str(result2)])

nowadays I'm sure there is a better way, but it works well for me, even though you have to design the layout details manually.

Fredriku73
+5  A: 

First, you will have to make sure you suppress any output to the command window. You can do this by making sure you end lines with a semicolon, avoid using the DISP function to display data, and avoid using the FPRINTF function to send data to the standard output (i.e. command window).

Second, determine what sort of "results" you are wanting to display. If it is a vector or matrix of numeric values, you may want to use a UITABLE object instead of a static text box (as suggested in Azim's answer to your other question, and assuming you have one of the newer versions of MATLAB). If it is just a couple of numeric values, characters, or strings, then I would suggest using a static text box. For example:

hList = uicontrol('Style','text','Position',[100 100 200 200]);
set(hList,'String',{'Line 1'; 'Line 2'});  % Displays 2 lines, one string each
vec = rand(3,1);  % Column array of 3 random values
set(hList,'String',num2str(vec));  % Displays 3 lines, one number per line

With this option, you will probably end up doing a lot with string operations.

NOTE: With static text boxes, if you put more text in them than they are able to display, they will simply cut off the text. They do not automatically create sliders to view the whole piece of text. You will either have to make the static text box bigger, adjust the "FontSize" property of the static text box to a smaller value, or (the more complicated option) create your own slider which will adjust what is displayed in the text box.


EDIT: The more complicated option...

In case anyone is interested, I thought I'd include some sample code for making a static text box with a slider that controls the contents displayed in the text box. There are quite a few different ways to do this, ranging from the simplest implementation I can think of (given below) to more complicated versions using nested functions and fancy OOP stuff.

First, you will have to have the two following functions saved as m-files:

callback_scrolltext.m

function callback_scrolltext(source,event,hText)
  textString = get(hText,'UserData');
  nLines = numel(textString);
  lineIndex = nLines-round(get(source,'Value'));
  set(hText,'String',textString(lineIndex:nLines));
end

update_scrolltext.m

function update_scrolltext(newText,hText,hSlider)
  newText = textwrap(hText,newText);
  set(hText,'String',newText,'UserData',newText);
  nRows = numel(newText);
  if (nRows < 2),
    sliderEnable = 'off';
  else
    sliderEnable = 'on';
  end
  nRows = max(nRows-1,1);
  set(hSlider,'Enable',sliderEnable,'Max',nRows,...
      'SliderStep',[1 3]./nRows,'Value',nRows);
end

Second, create the GUI objects with the following code. You can set the 'Position' properties to whatever you want, as well as use any handle for a figure or panel object in place of hParent:

hParent = figure;
hText = uicontrol('Style','text',...
                  'Parent',hParent,...
                  'Units','pixels',...
                  'Position',[100 100 100 40]);
hSlider = uicontrol('Style','slider',...
                    'Parent',hParent,...
                    'Units','pixels',...
                    'Position',[200 100 10 40],...
                    'Enable','off',...
                    'Callback',{@callback_scrolltext,hText});

Finally, every time you want to change the text of the text box, call update_scrolltext with the text you want to display (contained in a cell array just like the string input to TEXTWRAP is) and the handles of the text box and slider. Here are some examples to try:

update_scrolltext({'hello'},hText,hSlider);
update_scrolltext({'hello'; 'there'; 'silly'; 'world'},hText,hSlider);

Modify the code as you see fit and enjoy! =)

gnovice
+2  A: 

I use listbox to display the command output just fine. I find that the scroll bars make viewing the output better without clutter.

Supposing status is the handle to the uicontrol with a style of listbox;

message = 'New output to be appended';
set(status,'String', [message; get(status,'String')]);

For this to work, I guess the initial string of the uicontrol should be a cell, and not a string. You can make sure of this by making the string of the uicontrol as 'Ready' on two lines, or you can coerce the string to a cell like this:

set(status,'String', [message; {get(status,'String')}]);
ustun
thank you all guys, I will give it a try.
one more thing, if you are updating the GUI during the code is running, the changes might not be immediately visible. Use 'drawnow' in that case to force redrawing the GUI.
ustun
A: 

I'm just starting in on Matlab Gui's, so I'm by no means any sort of expert or even experienced- but someone mentioned in another forum that the edit-text element automatically generates scroll-bars? It also has the added advantage of being selectable and copy-able I believe. I'm about to find out, if I can figure out my code. I'll post back when I know.

AllenH

related questions