Problem using GUIDE

6 views (last 30 days)
Dopgp
Dopgp on 28 May 2012
Hello. i am a new user of matlab and i am trying to use the GUIDE to create an interface to an existing program.
the program works perfectly on its own but when i try to use the interface to get the variables i run into
??? Undefined function or variable "EQ_PROT".
any help will be appreciated. Any more info you need i will try to answer asap
Thank you very much

Accepted Answer

Walter Roberson
Walter Roberson on 28 May 2012
The FAQ won't open? That is odd. But here is a copy of what it says:
How can I share data between callback functions in my GUI(s)?Edit
Unlike custom functions that you write, the callback functions take the predetermined input variables (hObject, eventdata, handles) so it's not clear how you can pass in other variables that you need to. There are several techniques you can use to share data between callback functions in your GUI. Which approach is easiest depends on whether you are creating your GUI using GUIDE to design the layout (GUIDE-based GUIs) or whether you are calling FIGURE, AXES, UICONTROL, etc. directly (programmatic GUIs.)
The main techniques are:
Using the handles structure. You can dynamically add on new members to the handles structure that contain your variables that you want to pass in. Unlike global variables, which expose the variables only where you put the global statement, attaching to the handles structure and passing handles will share all the variables you have attached to the handles structure. This could expose variables to your function that you did not need or want exposed. Since variables passed in to MATLAB functions are "pass by value" and not "pass by reference" if you change any of the variables, you are only changing the local copy. If you change any of the variables attached as members of the handle structure, and you want to retain these changed values in the calling function, you will have to return the handles structure as an output of your function, and accept it in your calling function, or else use the guidata() function. Otherwise changes you make to the handles structure variables are only local and will not survive once the function has returned.
% Add a brand new variable on to the handles structure.
handles.myNewVariable = 42;
Creating your callback functions as nested functions inside the main GUI function. The function must be truly nested with the main function's "end" statement and not merely listed as another separate, independent function just defined in the m-file. (There is a difference.)
Storing data in a property [often the UserData property] of a component in the GUI and retrieving it when needed. For example
set(handles.button1, 'UserData', myString).
Storing data in the application workspace using the SETAPPDATA and GETAPPDATA functions.
% Do this to save variables to your figure's workspace.
% handles.GUIHandle is the "Tag" property of your main GUI figure.
% Double-click figure to bring up the "Property Inspector" in GUIDE.
setappdata(handles.GUIHandle, 'yourVariable', yourVariable)
% Do this to retrieve variables from your figure's workspace.
yourVariable = getappdata(handles.GUIHandle , 'yourVariable')
% Do this to remove what you saved from your figure's workspace.
rmappdata(handles.GUIHandle, 'yourVariable')
Writing the data out to a file, such as a mat file with the save function, and then having the called function read in that mat file.
% Do this to save variables to your figure's workspace.
% yourVariable could be a single variable that is a structure
% with a bunch of variables as members of the structure
% if you wish. That can be convenient because then there
% is only one variable to save.
save(fullMATFileName, 'yourVariable')
% Do this to retrieve the variable.
storedStructure = load(fullMATFileName);
yourVariable = storedStructure.yourVariable;
Use the assignin() function.
% Send the variable myGUIVariable from the GUIs workspace
% to a variable called myBaseVariable in the "base" workspace.
assignin('base', 'myBaseVariable', myGUIVariable);
% They could both be the same name if you wish.
assignin('base', 'myVariable', myVariable);
Sharing between multiple GUIs. If the "main" GUI calls other GUIs, then the best way to do it is by passing variables in via the input argument list, and accepting output variables via the output argument list. The output argument of GUI2 can then be sent into GUI3. So someplace in GUI1 (like the callback function of the "Go!" button of GUI1), you'd have this
[out2a out2b out2c] = gui2(in2a, in2b, in2c, in2d);
[out3a out3b] = gui3(out2a, out2b);
or something along those lines. The arguments can be extracted out of the varargin cell array of your opening code for the GUI, for example in the GUI's "OpeningFcn" function if you used GUIDE. Once they are in your opening function, then they can be shared amongst the other functions in the GUI with the methods mentioned earlier in this section. This method will not let GUI1 control GUI2 and GUI3's parameters "live" - they can be changed only when calling the GUIs. To have GUI1 control GUI2 when GUI2 is already running, you can use the assignin() function.
Official Documentation. The documentation contains instructions for using these techniques to share data between callbacks in a GUIDE-based GUI ( <http://www.mathworks.com/access/helpdesk/help/techdoc/creating_guis/f5-998197.html>) and in a programmatic GUIs ( <http://www.mathworks.com/access/helpdesk/help/techdoc/creating_guis/f13-998197.html>).

More Answers (6)

Walter Roberson
Walter Roberson on 28 May 2012

Dopgp
Dopgp on 28 May 2012
that link is directing me to a general FAQ and not to the specific one. in the general FAQ there is the reference to your link as well but it wont open either.

Dopgp
Dopgp on 28 May 2012
this is the callback i am using to get the function name
function fontealt_Callback(hObject, ~, handles)
fontealt = str2double(get(hObject, 'String'));
handles.metricdata.fontealt = fontealt;
guidata(hObject,handles)
and the problem happens on this part of the code
if(handles.metricdata.fontealt=='1')
  1 Comment
Walter Roberson
Walter Roberson on 28 May 2012
I do not see any reference to EQ_PROT in that code, and you do not appear to have any "if" in that code. It is not clear which problem you are reporting against which line.
Are you trying to use handles.metricdata.fontealt in your main routine after the callback has set it? If so then have you used handles = guidata(gcf) to update the main routine's idea of what "handles" looks like?

Sign in to comment.


Dopgp
Dopgp on 28 May 2012
yea i was taking a closer look and the problem is in the EQ_PROT matrix.
??? Undefined function or variable "EQ_PROT".
not in the code section i posted earlier
the EQ_PROT matrix is defined as such
if(handles.metricdata.fontealt=='1')
ALIM_ALT = [0 0 1 0 0 0];
EQ_PROT = [0 1 1 2 2 2];
handles.metricdata.fusiveis = '1';
and is defined in other cases in similar ways. not sure why it says it is undefined thought. (if i fix the matrix as EQ_PROT = [0 1 1 2 2 2]; it works properly)
  1 Comment
Walter Roberson
Walter Roberson on 28 May 2012
We will need the code in context, including the routine that defines EQ_PROT and including the routine that has problems accessing it.

Sign in to comment.


Dopgp
Dopgp on 29 May 2012
hey walter i shared the program with you on docs.google.com if you would be as kind and take a look at it, to maybe give me a hand.
  1 Comment
Walter Roberson
Walter Roberson on 29 May 2012
When you send someone files in email, you should send a message saying what the files are about. The message showed up in my Junk folder with no explanation; I came close to deleting it.
I am not likely to have time to open a .fig file in the next month and a half (I do not have access to MATLAB at home.) You are likely to get a much faster response if you post the relevant code here.

Sign in to comment.


Dopgp
Dopgp on 29 May 2012
fair enough... i actually solved the problem as the program was not entering a if routine... but now the answers are coming up as NaN... and i cant find the issue. thank you very much thought =)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!