Passing Parameters from GUI to Script

2 views (last 30 days)
I have GUI that I'm working on as a front end to an existing script. A partner of mine helps with the script, so I would like to keep it as a separate file. I'm attempting to pass the GUI handles as a struct object to the script, the script then takes the values from the handles and creates variables in the base workspace which will be used by a Simulink model.
When the user has input all the parameters, they hit a "Go" button which then calls the other script.
I've read other forum postings on this, and the closest I've come to success if the following callback:
% --- Executes on button press in Go.
function Go_Callback(hObject, eventdata, handles)
assignin('base','hnd',handles);
setup;
When the "Go" button is pressed, it generates an error:
Undefined variable "hnd" or function "hnd.Load_Sin".
Error in setup (line 66)
csim.motor3_for_test = get(hnd.Load_Sin,'Value'); %NO
It doesn't matter what object I try to call from hnd, the first one in the script always generates an error.
The odd thing is, if I then go an manually run setup.m, it runs fine.
It's acting like it's running setup.m before the assignin command, even though assignin is clearly before it.
I may have other end users who will run this script and I don't want them to have to jump through hoops to get it to run. What do I need to do so it executes correctly from pressing the Go button?

Accepted Answer

Matt Fig
Matt Fig on 6 Sep 2012
Edited: Matt Fig on 6 Sep 2012
If setup is a script, you are actually running it from in the callback. If that is o.k., try this:
function Go_Callback(hObject, eventdata, handles)
hnd = handles;
setup;
if not, you will have to do this:
function Go_Callback(hObject, eventdata, handles)
assignin('base','hnd',handles);
evalin('base','setup;');
  1 Comment
Alan
Alan on 6 Sep 2012
D'oh. Just had a Homer Simpson moment.
Originally, I had this: csim.motor3_for_test = get(handles.Load_Sin,'Value');
Which is very similar. On searching the internet I thought it was because my script couldn't access the GUI's workspace.
What I didn't notice until I tried your suggestion, is that the error was NOT on the first line when I did that. I had typed a couple variables wrong further down the line - lower case where it should've been upper case! It was giving an error something like "nonexistent field" (not getting it anymore so I don't have it to copy).
What I still find odd is that it didn't seem to care about the case when I ran setup.m by itself - only when called from within the callback. That is truly bizarre.
Anyway, thank you!

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!