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

2 views (last 30 days)
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

Accepted Answer

Geoff Hayes
Geoff Hayes on 23 Mar 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 Comments
Oscar Espinosa
Oscar Espinosa on 27 Mar 2018
Thank you for the help Geoff,the example that you send me really helped, now I can retrieve data from the others UIs.

Sign in to comment.

More Answers (0)

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by