MATLAB Answers

Undefined function or variable (Passing data between functions)

3 views (last 30 days)
Oscar Espinosa
Oscar Espinosa on 22 May 2018
Commented: Oscar Espinosa on 22 May 2018
Hi, I got this problem. When storing data in a function and then I trying to retrieve it 'Undefined function or variable 'GI'' occurs.
This is the code in which I get the data from a GUI (It works well):
function [GI]=GUI_INP(hObject, eventdata)
%Get the handle of Input_parameters and Geometrical_characteristics
hfig_i_p = findobj('Tag','Initial_parameters');
%If exists (not empty)
if ~isempty(hfig_i_p)
i_p=guidata(hfig_i_p);
%ТТХ
GI.Vpol=i_p.Vpol_value;
%Полетные параметры
GI.plotn=i_p.plotn_value; %плотность воздуха
else
disp('INGRESE LOS DATOS EN I_P Y G_C');
end
end
And then the code in which I try to retrieve that data:
function [sys,Inp,geom_aircraft]=Z_INP(GI)
%ТТХ
Inp.Vpol=GI.Vpol;
%полетные параметры
Inp.plotn=GI.plotn; %плотность воздуха
end
I will aprecciate the help.

  5 Comments

Show 2 older comments
Nicola Bombace
Nicola Bombace on 22 May 2018
The question would then be if you see the Variable GI in the workspace after you call the GUI_INP function. If the variable is not there is because this condition ~isempty(hfig_i_p) is false.
Oscar Espinosa
Oscar Espinosa on 22 May 2018
Is in the workspace while function executing, then just return ans with the values Vpol and plotn. If I try executing in the Command Window I got 'Undefined function or variable 'hObject''.
Oscar Espinosa
Oscar Espinosa on 22 May 2018
Thank for help, I found the problem. The function do not need hObject and eventdata.
function [GI]=GUI_INP(hObject, eventdata)

Sign in to comment.

Answers (1)

Jan
Jan on 22 May 2018
Edited: Jan on 22 May 2018
How do you call these functions?
GI = GUI_INP(hObject, eventdata);
[sys,Inp,geom_aircraft] = Z_INP(GI);
This should work. At least partially, because sys and geom_aircraft are still undefined.
The code will fail, when the wanted figure is not found. Better use error() with a clear message, instead of just a disp(), because the following code will fail with confusing messages.
If you have a lot of open figures containing many objects, this
hfig_i_p = findobj('Tag','Initial_parameters');
will take some time, because it compares the Tags of all GUI-elements. Specify the object type instead:
hfig_i_p = findobj(allchild(groot), 'flat', 'Tag', 'Initial_parameters', 'Type', 'Figure');
Currently this is redundant, because the children of groot are figures only. But maybe this will change in the future.

  2 Comments

Oscar Espinosa
Oscar Espinosa on 22 May 2018
Actually I'm calling
[sys,Inp,geom_aircraft] = Z_INP(GI);
from another funtion. I'm doing this intermediate step between the data stored in a GUI and the function that needs that data because the function to execute have 8 input, and if I add the hObject, and evenData as 2 more arguments it just does not work.
This is the function which is calling:
function [Result,Inp,lz,nu,str,c_,f0,fk]=m(s_lz,s_nu,s_srt,s_ak,s_c,s_c_k_koef,s_f0,s_fk);
[lz,nu,str,ak,c_,c_k_koef,f0,fk]=INP(s_lz,s_nu,s_srt,s_ak,s_c,s_c_k_koef,s_f0,s_fk);
shet=1;
for s=1:length(lz)
Inp.lz=lz(shet,1);
Inp.nu=nu(shet,1);
Inp.str=str(shet,1);
Inp.a_k=ak(shet,1);
Inp.c_=c_(shet,1);
Inp.c_k_koef=c_k_koef(shet,1);
Inp.f_0=f0(shet,1);
Inp.f_k=fk(shet,1);
[sys,Inp,geom_aircraft]=Z_INP(GI);
...
end
Oscar Espinosa
Oscar Espinosa on 22 May 2018
Thank you by the help, I found the problem, posted it lines above. And I will keep in mind the advices that you gave me.

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!