Calling userdefined value from GUI .m file into function .m file

Hello all, I need help in calling user defined value from GUI .m file to function .m file. I have GUI .m file which call different functions to compute the loads by clicking a push button.
Now I am looking for a way to get a user defined value which will be used in one of the function .m files to give the solution.
I tried with handles and had no luck. This is what I tried: In GUI .m file I have a drop down to collect the user defined input. This is the code:
M2.material2=3;
prompt = {'Youngs Modulus (MPa or N/mm2)','Poissions ratio'};
dlg_title = 'Material Properties';
num_lines = 1;
defaultans = {'',''};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
assignin('base','YoungsMod2',str2double(answer{1}));
assignin('base','PoissionsRat2',str2double(answer{2}));
M2.YoungsMod2=str2double(answer{1});
M2.PoissionsRat2=str2double(answer{2});
And I have this code after the above; end
set(handles.surf_material2, 'UserData', M2);
Now in the function .m file where I have defined the calculation for the above input, I have added this:
M2= get(handles.surf_material2, 'UserData');
But when I execute the code I am getting this error:
Error while evaluating uicontrol Callback
Undefined variable "handles" or class "handles.surf1_material".
Error on line:
M2= get(handles.surf1_material, 'UserData');
Can anyone guide me to solve this issue?

5 Comments

adi - please provide more details on the function .m file where I have defined the calculation for the above input. Is it a function that you call from your GUI? If it is, then why don't you pass in the parameter(s) as inputs to this function?
Trying to call
M2= get(handles.surf_material2, 'UserData');
from within your function when you haven't passed in the handles structure is not going to work because this variable is not defined and so the error message makes sense.
Also, why are you saving data to the workspace with
assignin('base','PoissionsRat2',str2double(answer{2}));
Please provide some context surrounding your GUI, the workspace, and the function that you are calling.
Yes. It's the function that I call from my GUI. Can you show me an example when you say "why don't you pass in the parameter(s) as inputs to this function" ?
Change the signature of your function to pass in the value. If your function is called myFunction then add whatever parameters you need
function [someOutput] = myFunction(M2)
% your code
Then call this function from your GUI as
myFunction(get(handles.surf1_material, 'UserData'))
or however you need to get the M1 or M2 data.
M1 and M2 are taken from GUI by a drop down menu and used in function which is again called by GUI. So I am not sure how I use the above code for the same?
adi - how are you currently calling this function? Please copy and paste all relevant code so that we can see how it is being called. If it is being called from a GUI callback, then please copy and paste all code within this callback.

Sign in to comment.

Answers (0)

Categories

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

Tags

Asked:

on 3 Aug 2016

Commented:

on 3 Aug 2016

Community Treasure Hunt

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

Start Hunting!