Help: Display a different layout when pushing a button in a GUI?

Hi all,
I'm trying to write a programmatic GUI. It includes a push button and a edit text named "tab1" when it initializes. Then when I hit the button the edit text "tab1" will be hidden and the GUI will show the edit text "tab2" which was invisible before hitting the button. I also attached my code which doesn't work.
Any help would be greatly appreciated.
Thanks.

2 commentaires

Which Matlab version are you using?
Hi Robert
I'm using MATLAB 2014a.

Connectez-vous pour commenter.

 Réponse acceptée

Robert Cumming
Robert Cumming le 17 Oct 2014
The reason your code doesn;t work - is that when the callback for the pushbutton is created the variable S.tab1 doesn't exist. If you moved the pushbutton creation to the end then it would work.
It looks like you are trying to build a GUI with tabs?
If you looking for an off the shelf solution look here
A good place to start to learn about GUIs is to use this FEX submission

1 commentaire

Khanh
Khanh le 17 Oct 2014
Modifié(e) : Khanh le 17 Oct 2014
I'm trying to build a GUI with multi tab. Really thank you. I moved the set callback for the push button on the callback function line immidiately, it worked.
Thanks for your addtional advice.

Connectez-vous pour commenter.

Plus de réponses (1)

Mikhail
Mikhail le 14 Oct 2014
Didn't see your code. In your push button callback function write: set("edit box id",'String','tab2'), where "edit box id" is your edit box identifier (It appears when you create it.)

5 commentaires

function [] = untitled_build(varargin)
% --- FIGURE -------------------------------------
S.figure1 = figure( ...
'Tag', 'figure1', ...
'Units', 'characters', ...
'Position', [102.8 24.3076923076923 114 32.9230769230769], ...
'Name', 'untitled', ...
'MenuBar', 'none', ...
'NumberTitle', 'off', ...
'Color', [0.941 0.941 0.941])
% --- PUSHBUTTONS -------------------------------------
S.pushbutton1 = uicontrol( ...
'Tag', 'pushbutton1', ...
'Style', 'pushbutton', ...
'Units', 'characters', ...
'Position', [37.2 11.2307692307692 23 3.23076923076923], ...
'String', {'Push Button'})
set(S.pushbutton1,'callback',{@pushbutton1_Callback,S})
% --- EDIT TEXTS -------------------------------------
S.tab1 = uicontrol( ...
'Tag', 'tab1', ...
'Style', 'edit', ...
'Units', 'characters', ...
'Position', [14.8 23.6153846153846 24.8 3.92307692307692], ...
'BackgroundColor', [1 1 1], ...
'String', 'tab1')
set(S.tab1,'callback',{@tab1_Callback,S})
S.tab2 = uicontrol( ...
'Tag', 'tab2', ...
'Style', 'edit', ...
'visible','off',...
'Units', 'characters', ...
'Position', [59.6 23.4615384615385 24.8 3.92307692307692], ...
'BackgroundColor', [1 1 1], ...
'String', 'tab2')
set(S.tab2,'callback',{@tab2_Callback,S})
%%---------------------------------------------------------------------------
function []=pushbutton1_Callback(varargin) %#ok<INUSD>
S=varargin{3}
%set(S.tab1,'visible','off')
%
% %%---------------------------------------------------------------------------
% function tab1_Callback(hObject,evata) %#ok<INUSD>
%
%
%
% %%---------------------------------------------------------------------------
% function tab1_CreateFcn(hObject,evata) %#ok<INUSD>
%
%
%
% %%---------------------------------------------------------------------------
% function tab2_Callback(hObject,evata) %#ok<INUSD>
%
%
%
% %%---------------------------------------------------------------------------
% function tab2_CreateFcn(hObject,evata) %#ok<INUSD>
%
%
%
The attached file is OK. But I also post it in comment. Please examine and figure out my mistake.
function []=pushbutton1_Callback(varargin) %#ok<INUSD>
S=varargin{3}
%set(S.tab1,'visible','off')
This is your pushbutton callback function. To make your tab2 visible write inside: set(S.tab2,'visible','on')
To make your tab1 invisible write inside:
set(S.tab1,'visible','off')
Note: you might have problem with arguments. Either use global variables (callback function is inside your main function) or at the step where you define your callback function for pushbutton insert additional arguments S.tab1, S.tab1
Hi Mikhail
Thanks for you answers.
Although I used both ways, it doesn't work. I went around whole day but I still don't understand my problem.
Hi, no problem. What I recommend to you is to download something in Matlab exchanger and use it as a template for a first time, before you get enough experience.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by