Error using get, Invalid handle

5 views (last 30 days)
Palumbo Piero
Palumbo Piero on 26 Oct 2017
Commented: Rik on 27 Oct 2017
Good morning everyone,
I am working on a GUI and I have a problem with a popup-menu. If I want to interact with the GUI and choose another fluid, the program doesn't run and it gives me the following error:
Error using get
Invalid handle
Error in test2>fluid_Callback (line 163)
val=get(handles.fluid,'value');
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in test2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)test2('fluid_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
This is the code about the popup-menu:
% --- Executes on selection change in fluid.
function fluid_Callback(hObject, eventdata, handles)
% hObject handle to fluid (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns fluid contents as cell array
% contents{get(hObject,'Value')} returns selected item from fluid
% Determine the selected data set.
val=get(handles.fluid,'value');
switch val
case 1
fluid='R134a';
case 2
fluid='r410a.mix';
case 3
fluid='water';
case 4
fluid='ethylene';
case 5
fluid='CO2';
case 6
fluid='ethanol';
end
% Get the GUI handles
my_guidata=guidata(gcf);
% Add "fluid" to the handles
my_guidata.fluid=fluid;
% Store the updated GUI handles
guidata(gcf,my_guidata);
Thanks for your attention. Have a good day
[SL: edited to apply code formatting]

Answers (2)

Steven Lord
Steven Lord on 26 Oct 2017
val=get(handles.fluid,'value');
The first time you run this code, handles.fluid is the handle to one of the components in your UI.
I'm skipping the switch / case section
% Get the GUI handles
my_guidata=guidata(gcf);
% Add "fluid" to the handles
my_guidata.fluid=fluid;
% Store the updated GUI handles
guidata(gcf,my_guidata);
You just updated the handles structure in your GUI. The struct you used to perform this update overwrote the fluid field in the "master copy" of your GUI's handles structure. The next time this callback runs, handles.fluid will be 'R134a' or 'water' or the like. 'R134a' is not the handle to one of the components in your UI anymore, so it doesn't have a property named 'value'.
  2 Comments
Palumbo Piero
Palumbo Piero on 27 Oct 2017
I don't know why but i have used the same code also for another popup-menu and it works even when I interact with the GUI.
if true
% --- Executes on selection change in correlation.
function correlation_Callback(hObject, eventdata, handles)
% hObject handle to correlation (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns correlation contents as cell array % contents{get(hObject,'Value')} returns selected item from correlation
val=get(handles.correlation,'value'); switch val case 1 a=1; set(handles.Validation,'String','Vertical and horizontal flow in tubes and annuli, Water/R11/R22/R113/R114/Ethylene/Glycol, d=2.95-3.2mm, p=0.08-20.6bar'); case 2 a=2; set(handles.Validation,'String','Horizontal macro and mini-channels:d=1.5,3.0,6.61,7.49 mm, R410A, G=100-600 kg/m2s, q=10-40 kW/m2, Tsat=5-15°C, x=0.2-1'); case 3 a=3; set(handles.Validation,'String','Horizontal minichannels: d=1.5,3.0 mm , L=2000 mm, R22,R134a,CO2, q=10-40 kW/m2, G=200-600 kg/m2s, Tsat=10°C') case 4 a=4; set(handles.Validation,'String',' No condition found') case 5 a=5; set(handles.Validation,'String','Horizontal tube d=8 mm, R22,R134a,R125,R410A, Tsat=25-45°C, Pred=0.19-0.53, G=200-600 kg/m2s, q=9-53 kW/m2') case 6 a=6; set(handles.Validation,'String','R141b d=1.39-3.69 mm') case 7 a=7; set(handles.Validation,'String',' d=3.1mm G=125-750 kg/m2s q=14-380 kW/m2 P=1.3-4.1 bar R113') case 8 a=8; set(handles.Validation,'String','Micro channel, Water,R134a, q=159-938 kW/m2, G=127-654 kg/m2s, p=1.44-6.60 bar, x=0.26-0.87') case 9 a=9; set(handles.Validation,'String','Water,refrigerants, Ethylene glycol, ethanol, d=2.95-32.0 mm, G=12.4-8179.3 kg/m2s, q=348.9-2.62*10.^6 W/m2, x=0-0.948, Re_lo=568.9-8.75*10.^5, p_red=0.0023-0.895, Pr_lo=0.83-9.1, Fr=2.66*10.^-4-2240') case 10 a=10; set(handles.Validation,'String','Vertical tubes: d=0.52,1.1,2.01,2.88,4.26 mm, G=100-500 kg/m2s, q=2.4-175.4 kW/m2, p=6-14 bar') case 11 a=11; set(handles.Validation,'String','d=0.21-6.05 mm, G=44-1500 kg/m2s, q=5-109 kW/m2') case 12 a=12; set(handles.Validation,'String','d=2.4-2.92 mm, G=44-852 kg/m2s, q=7.5-129 kW/m2, R12/R113/R134a') case 13 a=13; set(handles.Validation,'String','Small rectangular channels, d=0.75 mm, 2.7*10^-4<=Bo<=8.9*10^-4, 0.03<=x<=0.55, FC84') case 14 a=14; set(handles.Validation,'String','Horizontal tube d=2.98 mm, p=200 kPa, G=50-200 kg/m2s')
end
my_guidata=guidata(gcf); my_guidata.a=a; guidata(gcf,my_guidata);
end
Rik
Rik on 27 Oct 2017
Select your code and click the {}Code button. Don't you see this is unreadable? Stephen and Steven both already edited your question/comment, why don't you try it yourself this time?
As far as I can read you code, it never overwrites the handle, but it uses only set(handles.Validation,'String', something), which is not how your code does it.

Sign in to comment.


Rik
Rik on 26 Oct 2017
Have a read here and here. It will make it easier for others to answer your question (and for you to answer your own).
You are overwriting the handle to your dropdown menu with a string. So next time you try to get properties from the object, get is getting an invalid handle: a string.
  4 Comments
Palumbo Piero
Palumbo Piero on 27 Oct 2017
Edited: Stephen23 on 27 Oct 2017
% --- Executes on selection change in fluid.
function fluid_Callback(hObject, eventdata, handles)
% hObject handle to fluid (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns fluid contents as cell array
% contents{get(hObject,'Value')} returns selected item from fluid
% Determine the selected data set.
val=get(handles.fluid,'value');
switch val
case 1
fluid='R134a';
case 2
fluid='r410a.mix';
case 3
fluid='water';
case 4
fluid='ethylene';
case 5
fluid='CO2';
case 6
fluid='ethanol';
end
% Get the GUI handles
my_guidata=guidata(gcf);
% Add "fluid" to the handles
my_guidata.fluid=fluid;
% Store the updated GUI handles
guidata(gcf,my_guidata);
Stephen23
Stephen23 on 27 Oct 2017
@Palumbo Piero: today I formatted your code correctly for you. In future you can do it yourself: first select the code text, then click the {} Code button.

Sign in to comment.

Categories

Find more on Fluid Mechanics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!