Undefined function or variable (Passing data between functions)
Show older comments
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
Nicola Bombace
on 22 May 2018
Edited: Nicola Bombace
on 22 May 2018
If you try to run the file Z_INP.m , it will not know about the Variable GI. After you run the GUI gathering of your data simply use
[sys,Inp,geom_aircraft]=Z_INP(GI);
in a command line or a new script that could look like
[GI]=GUI_INP(hObject, eventdata);
[sys,Inp,geom_aircraft]=Z_INP(GI);
Oscar Espinosa
on 22 May 2018
Nicola Bombace
on 22 May 2018
Edited: 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
on 22 May 2018
Edited: Oscar Espinosa
on 22 May 2018
Oscar Espinosa
on 22 May 2018
Answers (1)
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.
Categories
Find more on Data Type Identification 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!