Error using get, Invalid handle
이전 댓글 표시
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]
답변 (2개)
Steven Lord
2017년 10월 26일
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
Palumbo Piero
2017년 10월 27일
Rik
2017년 10월 27일
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.
Rik
2017년 10월 26일
0 개 추천
댓글 수: 4
Palumbo Piero
2017년 10월 26일
Rik
2017년 10월 26일
Edit your question so people can read your code, then I can properly see what your code is doing. (select your code and press the {}Code button)
Palumbo Piero
2017년 10월 27일
편집: Stephen23
2017년 10월 27일
Stephen23
2017년 10월 27일
@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.
카테고리
도움말 센터 및 File Exchange에서 Foundation and Custom Domains에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!