Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass parameters to a Matlab GUI file


i am new to matlab. While working through the Matlab GUI, i faced a problem which is as follows.
.i want to have 2 figure files, with one figure file calling the other. i know that just by calling the name of the 2nd fig file from the first fig file, we can call the 2nd figure. however, i also wish to send some parameters from one fig file to another.here i need to send the arguments and also obtain these parameters so as to do further processing.i havent been able to find a solution to this problem. i would be glad if someone helps me out with this problem.
thanking you in advance

like image 230
jack1689 Avatar asked Mar 17 '11 23:03

jack1689


2 Answers

I would do like this (assuming you're using the GUI builder GUIDE).

Let's say that your figures/m-files are named firstFigure.fig/m and secondFigure.fig/m. In the code of firstFigure, just call secondFigure and pass your parameters as arguments:

someNumber = 1;
someText = 'test';
aMatrix = rand(3);

secondFigure(someNumber, someText, aMatrix);

The arguments will be available to secondFigure as a variable varargin in the callback functions

function varargout = secondFigure(varargin) 

and

function secondFigure_OpeningFcn(hObject, eventdata, handles, varargin)

varagin is a cell structure; use cell2mat and char to convert it back:

theNumber = cell2mat(varargin(1));
theText = char(varargin(2));
theTextAgain = cell2mat(varargin(2));
theMatrix = cell2mat(varargin(3));
like image 33
jonxon Avatar answered Oct 15 '22 04:10

jonxon


There are three ways I found to do this:

Method 1: Use setappdata and getappdata like so:

setappdata(0,'some_var',value)
some_other_var = getappdata(0,'some_var')

You would use setappdata() in the m-file for fig1 to store whatever data you wanted to pass around, and then call getappdata() in another m-file to retrieve it. The argument 0 to the two functions specifies the MATLAB root workspace, which is accessible by your program everywhere (i.e. it is global). As such, when you close your figures that data will still be available. You may want to use rmappdata to remove them.

Method 2: Use guidata:

Assuming you created your GUI with GUIDE, then you have access to a structure called handles which is passed around everywhere and which you can edit, and so you can do this in a GUI callback:

handles.some_var = some_value
guidata(hObject,handles)

Then you can access handles.some_var elsewhere in some other callback (because handles is automatically passed into it for you) in your other m-file:

some_other_var = get(handles.some_var)

Method 3: Use UserData:

Store the variable you want from your first figure:

set(name_of_fig, 'UserData', some_var)

Then to get it from your other one:

some_other_var = get(name_of_fig, 'UserData')  

(Disclaimer: My actual knowledge of MATLAB is not all that great, but it helps to be able to find good resources like this and this, and even this from the official docs. What I've written here may be wrong, so you should definitely consult the docs for more help.)

like image 140
zxt Avatar answered Oct 15 '22 05:10

zxt