GUI panel suddenly dissapear.

Hello, I'm new at making GUI.. and I maybe did some mistake in creating this GUI of mine.
So I want to create a panel which automatically appear/dissapear (So the GUI will like having a hidden panel, inside a hidden panel)
Here I create an if statement, where if the user choose "Mechanical" the mechanical panel will come out and if the user choose "N2 Gas Cushion", the Gas Cushion panel will appear. And if the user choose other than "N2 Gas Cushion", the Gas Cushion panel will dissapear.
Everything went well untill I add "setappdata(Master,'OilPreserver',OilPreserve_)", but after I add this line, when user choose "N2 Gas Cushion" suddenly the mechanical panel dissapear. But if I delete the setappdata() command, everything will work fine. But I need these command to pass the variable into the Master figure so I can save it into a *.mat file and pass it on another procedure/function.
Maybe anyone here can help me solve the problem..
Here is my code. Thank you very much.
% The GUI intro created using GUIDE command on matlab
.
.
% --- Executes just before guidetemplate0 is made visible.
function guidetemplate0_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to guidetemplate0 (see VARARGIN)
% Choose default command line output for guidetemplate0
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes guidetemplate0 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
set(handles.GeneralPanel, 'Visible', 'off');
set(handles.MechanicalPanel, 'Visible', 'off');
set(handles.ElectricalPanel, 'Visible', 'off');
set(handles.AccesoriesPanel, 'Visible', 'off');
set(handles.RectifierOperationPanel, 'Visible', 'off');
set(handles.GasCushionPanel, 'Visible', 'off');
.
% A bunch of code
.
function OilPreserveType_Callback(hObject, eventdata, handles)
% hObject handle to OilPreserveType (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str = get(hObject, 'String');
val = get(hObject,'Value');
% Set current data to the selected data set.
switch str{val};
case 'Integrally Filled' % User selects peaks.
OilPreserve_ = 1;
handles.OilPreserve = OilPreserve_;
set(handles.GasCushionPanel, 'Visible', 'off');
case 'N2 Gas Cushion' % User selects membrane.
OilPreserve_ = 2;
handles.OilPreserve = OilPreserve_;
set(handles.GasCushionPanel, 'Visible', 'on');
case 'Conservator'
OilPreserve_ = 3;
handles.OilPreserve = OilPreserve_;
set(handles.GasCushionPanel, 'Visible', 'off');
end
% Save the handles structure.
guidata(hObject,handles);
setappdata(Master,'OilPreseve',OilPreserve_);
% Hints: contents = cellstr(get(hObject,'String')) returns OilPreserveType contents as cell array
% contents{get(hObject,'Value')} returns selected item from OilPreserveType

Answers (1)

Using setappdata in another figure (Master is another figure, isn't it?) does not influence the visibility of GUI elements. This means, that the observed effect must be triggered somewhere else. Use the debugger to step through the code line by line to find out, where the the cause for the "sudden disapperaring" is.
The name "guidetemplate0" is confusing. This should be the name of the GUI.
By the way, the existence of the term "suddenly" in the question is highly correlated to a simple solution, which has been hard to find because the programmer searched at the wrong locations for the problem. A very interesting experience from the last 20 years of activity in the forums.
Some hints for a nice programming style: Avoid repeated commands:
switch str{val} % No semicolon behind SWITCH
case 'Integrally Filled' % User selects peaks.
OilPreserve_ = 1;
Visible = 'off';
case 'N2 Gas Cushion' % User selects membrane.
OilPreserve_ = 2;
Visible = 'on';
case 'Conservator'
OilPreserve_ = 3;
Visible = 'off';
otherwise % No SWITCH without OTHERWISE
error('Programming error: Unexepected SWITCH expression.');
end
handles.OilPreserve = OilPreserve_;
set(handles.GasCushionPanel, 'Visible', Visible);
I'm using a small tool:
function S = OnOffStr(T)
if any(T)
S = 'on';
else
S = 'off'
end
end
Then you can omit the Visible='off' commands and use:
set(handles.GasCushionPanel, 'Visible', OnOffStr(OilPreserve_ == 2));
This is shorter, but the explicit setting of the visibility in the switch cases is more clear: Then all setting are accessed in the same block of code and not distributed over different lines.

2 Comments

Chandra Winardhi
Chandra Winardhi on 12 Jan 2019
Edited: Chandra Winardhi on 12 Jan 2019
Hello thank you for your answer, I really appreciated it. I also have changed the repeated command and the code becomes shorter and easy to debug. I also change the guidetemplapte0 into Master. But the problems still exist.
Actually Master is not another figure, it is the name of the GUI figure that I created using Guide.. So I used setappdata on it. Maybe is this a problem? Sorry for asking a lot of thing.
Thank you very much for your time and help.
setappdata(FigH, ...) and guidata(FigH, handles) is equivalent. So it might be simpler to store the variables inside the handles struct.
If you change the name of callbacks, the confusion might be perfect. Did you adjust the callback names in the figure's objects also?
You can use the debugger to find the line, which disables the visibility. It cannot be the setappdata command directly, but it sounds like this triggers another code anywhere else.

Sign in to comment.

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Asked:

on 11 Jan 2019

Commented:

Jan
on 13 Jan 2019

Community Treasure Hunt

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

Start Hunting!