Help with GUI panels

14 ビュー (過去 30 日間)
aurc89
aurc89 2014 年 8 月 22 日
Hi ! I need to create a multiple GUI with Matlab, i.e. a GUI made of more panels, where each panel, containing more commands (push buttons, edit texts, axes...), can be visualized separately (hiding the other panels) by pressing a Push button. For example, I created a panel, called Panel_1, like this
Now I want to create another panel, let'say Panel_2, with other commands. Then, I want to visualize one of the two panels separately (and acting on its commands) by selecting an appropriate push button, like this:
Let's call uipanel1 and uipanel2 the tags associated to the two panels, and pushbutton_panel1 and pushbutton_panel2 the tags associated to the two push buttons. By pressing push button "Panel_1" I want to visualize the Panel_1 and act on its commands, by pressing push button "Panel_2" I want to visualize the Panel_2 and act on its commands. Of course, when working in Panel_2 I want to be able to recall functions associated to the commands contained in Panel_1. These are the callback functions I wrote, associated to the push buttons:
% --- Executes on button press in pushbutton_panel1.
function pushbutton_panel1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_panel1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.uipanel1,'visible','on')
set(handles.uipanel2,'visible','off')
% --- Executes on button press in pushbutton_panel2.
function pushbutton_panel2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_panel2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.uipanel1,'visible','off')
set(handles.uipanel2,'visible','on')
But it doesn't work: when I press Run, I see this:
When I press Push button 'Panel_1' , I see this:
and, when pressing Push button 'Panel_2', I see nothing:
Can you help me to solve this? Thanks!
  2 件のコメント
Andrew Reibold
Andrew Reibold 2014 年 8 月 22 日
編集済み: Andrew Reibold 2014 年 8 月 22 日
I have done the same thing before, and just tried a test case with your code and it worked fine for me. Make sure to try closing your figure and reopening and retrying, and also I would double check that both don't say the same command, or that you accidentally swapped on and off and 1 and 2 at the same time effectively making it the same. If you copy/pasted that, its not the problem.
set(handles.uipanel1,'visible','on')
set(handles.uipanel2,'visible','off')
I guess you could also double check that everything has the right tag names but looks like your example is defaults so that doesn't seem promising either.
If all else fails, I would try remaking the panels and starting over if they are not too complex. If you accidentally goofed something somehow or corrupted a save file, guis can be a real mess sometimes.
Adam
Adam 2014 年 8 月 22 日
Did you get any error message on the command line? e.g.
Reference to non-existent field 'uipanel2'.

サインインしてコメントする。

採用された回答

Joseph Cheng
Joseph Cheng 2014 年 8 月 22 日
編集済み: Joseph Cheng 2014 年 8 月 22 日
So what is happening in your examples is that you placed panel 2 inside panel 1. That is why in the initial panel contains both overlapping. Then when you're doing your panel 1 button callback you see the correct panel 1 (as you're hiding the overlapping panel 2) and nothing in the panel 2 button callback as you're hiding panel 1 (and everything inside of it, which includes panel 2).
To get around this is that you can overlap them by using the arrow keys when you highlight instead of using the mouse and drag them. When you use the mouse and drag one on top of the other it places one inside the other. hitting ctrl+arrow key makes a coarse move and just arrows does it by pixel.
Another approach is to instead of placing them ontop of each other use the set() and the position parameter to move the panels around (ie off window and out of sight and move the desired one into position). I do not know if there are down sides (performance wise) but i have done this in the past.
  3 件のコメント
Muhammad Salman Arif
Muhammad Salman Arif 2019 年 9 月 18 日
can you please ellaborate the use of set()
Muhammad Salman Arif
Muhammad Salman Arif 2019 年 9 月 18 日
Kindly mention if there is a way to show the first tab only in the begining?

サインインしてコメントする。

その他の回答 (1 件)

Robert Cumming
Robert Cumming 2014 年 8 月 22 日
編集済み: Robert Cumming 2014 年 8 月 22 日
This is a good example of why GUIDE is not really suitable at creating even mildly complex GUIs, when I started coding in Matlab I quickly moved away from GUIDE and started creating GUIs from code directly. Over the years I developed this extensively.
So much so that I have a GUI Toolbox which I license to clients - I am in the final stages of my own version of GUIDE to complement this - i.e. a multi tab/paged GUI which you can create interactively.

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by