Tuesday, November 25, 2014

Getting started with GUI in Matlab.

This is a short introduction on how use the Graphical User Interface (GUI) with Matlab:


To start a GUI in Matlab just type "guide" in the command window. You can then drag and drop object, and change their properties with a double click.

Load a file using a browser

Get the path of a file and load it:
[FileName,PathName] = uigetfile('*.mat','Select the MATLAB matrix');
str = load([PathName, FileName]);

working with variables

Matlab define a set of function which allow the user to define the possible interaction with the objects. This function can be access with the callback function (to generate it just do a right click on the object and go in the callback section).

One of the important issue with this methods is that all the data have to be loaded and saved each time you change of function.

The natural workflow is:

function pushbutton1_Callback(hObject, eventdata, handles)
% load the data
var = evalin('base', 'varName');
...
% manipulation
...
% save the data
assignin('base', 'varName', var);
or  getappdata and setappdata.

Get and set properties from a callback

To get the value of a property (for example the value of a slider):
get(handles.slider2, 'Value');
 and to set a property (for example to define the text of a edit object):
set(handles.edit1, 'string', 'text');
Sometimes finding the handles of an object can be a bit tricky. There is a special function called findobj which allow to do it. An easy way to use it it to look for the 'Tag'  of the object.
findobj('Tag''edit1'

Display images/graphs

Do display an image you have to add a axis object.
The next step is to select the axes.
axes(handles.axes1);
and finally to plot the image:
imagesc(img);

Ps: to remove the axis it is possible to use axis off


Save a file using a browser

The following function open a browser and allow to save a file
uisave('varName');

No comments:

Post a Comment