Error using get, Invalid handle
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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]
0 commentaires
Réponses (2)
Steven Lord
le 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 commentaires
Rik
le 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.
Voir également
Catégories
En savoir plus sur Aerospace Applications dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!