How to avoid errors when modifing data using guidata and get?

2 visualizaciones (últimos 30 días)
How can I modify and then store a value in editable box using guidata. In the future I will be need to retrieve that value in other GUI and maybe modify it also. I get the next error when executing for the second time a callback.
Error using handle.handle/get
Invalid or deleted object.
Error in Initial_parameters>button_1_callback (line 63)
i_p.Vpol=str2double(get(i_p.Vpol,'string'));
Error while evaluating UIControl Callback
The main function's code and the callback's function are the following:
function i_p = Initial_parameters(hObject,eventdata)
%Initial_parameters is a function in which you can store
%the velocity and density of the air
%
%Creation of the figure
fig = figure('units','pixels',...
'position',[50 500 400 100],...
'menubar','none',...
'name','Расчет АХ самолетов - Исходные данные',...
'numbertitle','off',...
'resize','off');
i_p = guihandles(fig);
%guidata(i_p.fig,i_p);
%Components
i_p.text1 = uicontrol('style','text',...
'units','normalized',...
'position',[0.05 0.6833 0.255 0.2666],... %'min',0,'max',2,... % This is the key to multiline edits.
'string','Скорость полета');
i_p.Vpol = uicontrol('style','edit',...
'units','normalized',...
'position',[0.355 0.6833 0.595 0.2666],... %'min',0,'max',2,... % This is the key to multiline edits.
'string','225',...
'tag','flight velocity');
i_p.text2 = uicontrol('style','text',...
'units','normalized',...
'position',[0.05 0.3666 0.255 0.2666],... %'min',0,'max',2,... % This is the key to multiline edits.
'string','Плотность воздуха');
i_p.plotn = uicontrol('style','edit',...
'units','normalized',...
'position',[0.355 0.3666 0.595 0.2666],... %'min',0,'max',2,... % This is the key to multiline edits.
'string','1.225',...
'tag','air density');
i_p.pushbutton_1 = uicontrol('style','push',...
'units','normalized',...
'position',[0.05 0.05 0.425 0.2666],...
'HorizontalAlign','left',...
'string','Сохранить',...
'callback',@button_1_callback);
i_p.pushbutton_2 = uicontrol('style','push',...
'units','normalized',...
'position',[0.525 0.05 0.425 0.2666],...
'HorizontalAlign','left',...
'string','Назад',...
'callback','close all;Main_menu');
%display([i_p.Vpol i_p.plotn]);
uicontrol(i_p.Vpol) % Give the editbox control.
guidata(fig,i_p);
end
%---------------------------------------
%Save the values
function button_1_callback(hObject,eventdata)
%button_1_callback is a function that change the value
%stored in Vpol and plotn
% Error using handle.handle/get
% Invalid or deleted object.
%
% Error in Initial_parameters>button_1_callback (line 58)
% i_p.Vpol=str2double(get(i_p.Vpol,'string'));
%
% Error while evaluating UIControl Callback
%
i_p=guidata(hObject);
i_p.Vpol=str2double(get(i_p.Vpol,'string'));
i_p.plotn=str2double(get(i_p.plotn,'string'));
display([i_p.Vpol i_p.plotn]);
guidata(hObject,i_p);
end

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 23 de Mzo. de 2018
Oscar - look closely at this line in your pushbutton1 callback
i_p.Vpol=str2double(get(i_p.Vpol,'string'));
You are getting the string value from the edit text box whose handle is i_p.Vpol AND then replacing this handle with the numeric equivalent of this string
i_p.Vpol=str2double(..
This means that on a subsequent call to the pushbutton1 callback, you will be using an invalid handle when trying to access the edit text control. You will want to save this result to some other field within the structure
i_p.Vpol_value = str2double(...
You may also want to prefix all of your handles with h to make it clear that it is a handle and not something that should be overwritten
i_p.hVpol = uicontrol('...
  4 comentarios
Oscar Espinosa
Oscar Espinosa el 27 de Mzo. de 2018

The error is the next:

Undefined function or variable 'hObject'.
Error in Z_INP (line 5)
i_p_value=guidata(hObject);
Error in run (line 64)
evalin('caller', [script ';']);
Error in Results_AERO (line 6)
run ('\\mscs-fs\data\310_user_data\3 факультет\3214\Espinosa Barcenas O.U\m0_17_11_17 -
GUI\Z_INP.m')
Error while evaluating uicontrol Callback

Basically first I need to open the Input_parameters (Исходные данные button) UI from this menu

,then I save the data, close the UI and open the next UI (Расчет АХ)but a function need to be executed with the saved data. Those are codes of the menu and the the function where the values are needed:

function Main_menu()
%Main_menu is our menu in which you can select, first store the values in
%the first row and then start with the calculation for the aerodynamic
%coeficients and aproximated empty weight of the aircraft
%
%Creation of the figure
fig = figure('units','pixels',...
                  'position',[0.0725 0.0725 400 100],...
                  'menubar','none',...
                  'name','Расчет АХ самолетов - Меню',...
                  'numbertitle','off',...
                  'resize','off');
menu = guihandles(fig);
%Figure in the center
scrsz = get(0, 'ScreenSize');
pos_act=get(gcf,'Position');
xr=scrsz(3) - pos_act(3);
xp=round(xr/2);
yr=scrsz(4) - pos_act(4);
yp=round(yr/2);
set(gcf,'Position',[xp yp pos_act(3) pos_act(4)]);
%---------------------------------------
%Components
%Initial parameters
menu.pb_i_p=uicontrol('Style','pushbutton', ...
                'Units','normalized', ...
                'Position',[0.05 0.6833 0.255 0.2666], ...
                'String','Исходные данные',...
                'Callback','Initial_parameters;');
%Geometrical characteristics 
menu.pb_g_c=uicontrol('Style','pushbutton', ...
                'Units','normalized', ...
                'Position',[0.355 0.6833 0.595 0.2666], ...
                'String','Геометрические характеристики',...
                'Callback','Geometrical_characteristics;');
%Aerodynamic calculation without deformation
menu.pb_AERO=uicontrol('Style','pushbutton', ...
                'Units','normalized', ...
                'Position',[0.05 0.3666 0.255 0.2666], ...
                'String','Расчет АХ',...
                'Callback',@Results_AERO);
%Aerodynamic calculation with deformation
menu.pb_AERO_for_def=uicontrol('Style','pushbutton', ...
                'Units','normalized', ...
                'Position',[0.355 0.3666 0.595 0.2666], ...
                'String','Расчет АХ с учетом деформации',...
                'Callback','clear all; close all;clc;');
%Empty weight calculation without deformation
menu.pb_m0=uicontrol('Style','pushbutton', ...
                'Units','normalized', ...
                'Position',[0.05 0.05 0.255 0.2666], ...
                'String','Расчет m0',...
                'Callback','clear all; close all;clc; Interface;');
%Empty weight calculation with deformation
menu.pb_5=uicontrol('Style','pushbutton', ...
                'Units','normalized', ...
                'Position',[0.355 0.05 0.595 0.2666], ...
                'String','Расчет m0 с учетом деформации',...
                'Callback','clear all; close all;clc;');
guidata(fig,menu);
end  

I just add the part of the code because is quite extense.

function [sys,Inp,geom_aircraft]=Z_INP()
sys.RASP='\\mscs-fs\data\310_user_data\3 факультет\3214\Espinosa Barcenas O.U\m0_17_11_17 - GUI';
sys.RASP1='\\mscs-fs\data\310_user_data\3 факультет\3214\Espinosa Barcenas O.U\m0_17_11_17 - GUI\ansys';
sys.Ansys='"C:\Program Files\ANSYS Inc\v182\ANSYS\bin\winx64\ansys182.exe"';
i_p_value=guidata(hObject);
%Геометрия
Inp.T=25;
Inp.m0=400000;
%Inp.m0=str2double(get(i_p.m0,'String')); %взлетная масса первого приближения (в килограммах)
Inp.P0=0.000627;
%Inp.P0=str2double(get(i_p.p0,'String'));%удельная нагрузка на крыло(кг/мм^2)
Inp.S=Inp.m0/Inp.P0;%площадь (в мм^2)
Inp.lz=8;
Inp.str=40;
Inp.nu=3;
%Inp.lz=str2double(get(handles.lz,'String')); %удлинение
%Inp.str=str2double(get(handles.str,'String')); %стреловидность
%Inp.nu=str2double(get(handles.nu,'String')); %сужение
Inp.str_25 = 27.3; %угол стреловидности на 0,25
Inp.PROF_type = 0; %0 - классический профиль, 1 - суперкритический профиль
Inp.S_WF_ = 0.15; %относительная подфюзеляжная площадь крыла
Inp.Lk=0.5*sqrt(Inp.S*Inp.lz);%0.5*sqrt(S*lz); %размах консоли крыла (в мм)12252.5;
Inp.b0=(Inp.S*2*Inp.nu)/(2*Inp.Lk*(1+Inp.nu));%(S*2*nu)/(2*Lk*(1+nu)); %корневая хорда (в мм)
Inp.bk=Inp.b0/Inp.nu;%b0/nu; %концевая хорда (в мм)
Inp.c_=0.137;
Inp.c_k_koef=1;
%Inp.c_=str2double(get(handles.c_,'String')); %относительная толщина профиля в корневой хорде 
%Inp.c_k_koef=str2double(get(handles.c_k_koef,'String'));%отношение c_k/c_ 
Inp.ck_=Inp.c_*Inp.c_k_koef;%0.09;%относительная толщина профиля в концевой хорде, 
%ТТХ
Inp.Lpol=4500; %дальность полета (в км)
%Inp.Vpol=225;
Inp.Vpol=i_p_value.Vpol; 
%Полетные параметры
Inp.alpha=4;
%Inp.alpha=str2double(get(handles.alpha,'String'));    %%угол атаки в градусах   
Inp.g=9.8; %ускорение свободного падения
Inp.Cp=0.6; %удельный расход топлива
%Inp.plotn=1.225;
Inp.plotn=i_p_value.plotn; %плотность воздуха
Inp.visc=1.98*10^-5; %Вязкость
Oscar Espinosa
Oscar Espinosa el 27 de Mzo. de 2018
Thank you for the help Geoff,the example that you send me really helped, now I can retrieve data from the others UIs.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by