MATLAB Answers

How to get a new GUI with components to open after closing another one using a pushbutton?

1 view (last 30 days)
Chris Gnam
Chris Gnam on 5 Feb 2016
Answered: Walter Roberson on 6 Feb 2016
So I am trying to create a GUI which does something simple, and then when you click submit, it will open up a new GUI and close the original. I have gotten it to do it's simple task, close itself and open up a new GUI... however the pushbutton that I am trying to put in the new GUI is not appearing, and I am getting an error message. I cannot, for the life of me, figure out what I am doing wrong! Here is the code I have:
function matrixinput %Generate the actual window prompt
matrixwindow = figure();
set(matrixwindow, 'MenuBar', 'none',...
'ToolBar', 'none',...
'Position', [450 450 200 140])
%Generate the main "label" text
text = uicontrol(matrixwindow, 'Style','text',...
'String','Please enter the 2x2 matrix values',...
'Position',[0 100 200 30]);
%Add in the text editing fields
Top_Left = uicontrol('Style', 'edit',...
'Position',[70 80 25 25]);
Top_Right = uicontrol('Style', 'edit',...
'Position',[100 80 25 25]);
Bottom_Left = uicontrol('Style', 'edit',...
'Position',[70 50 25 25]);
Bottom_Right = uicontrol('Style', 'edit',...
'Position',[100 50 25 25]);
%Add in submit button
submitbtn = uicontrol(matrixwindow, 'Style','pushbutton',...
'Position',[50 10 100 30],...
'Callback', {@submitbtn_Callback, Top_Left, Top_Right,...
Bottom_Left, Bottom_Right, matrixwindow});
function submitbtn_Callback(~, ~, Top_Left, Top_Right, Bottom_Left, Bottom_Right, matrixwindow)
%Collect the input values and define variables:
a = str2num(get(Top_Left, 'String'));
b = str2num(get(Top_Right, 'String'));
c = str2num(get(Bottom_Left, 'String'));
d = str2num(get(Bottom_Right, 'String'));
%Error message for no name being submitted
if isempty(a) | isempty(b) | isempty(c) | isempty(d)
msgbox('Please make sure all fields are properly filled out');
uiwait(msgbox); %Prevent the program from moving on until a valid name is entered
%Input the values into matrix form:
A = [a, b;
c, d];
disp(' Matrix A')
%Close first window once the submit button has been pressed...
matrixdisplay = figure(); %...Open new window
set(matrixdisplay, 'MenuBar', 'none',...
'ToolBar', 'none',...
'Position', [450 450 200 140])
%Generate the main "label" text
text = uicontrol(matrixdisplay, 'Style','text',...
'String','Please enter the 2x2 matrix values',...
'Position',[0 100 200 30]);
%Add in a test button
testbtn = uicontrol(matrixdisplay, 'Style','pushbutton',...
'Position',[50 10 100 30],...
'Callback', [@testbtn_Callback, matrixdisplay]);
function testbtn_Callback(~, ~, matrixdisplay)
When I run this program, everything works fine until the new GUI opens. Here is what is printed in the command window when I run the program, and input the values '1' '2' '3' and '4' into the first GUI prompt:
>> matrix_input
1 2
3 4 Matrix A
Error using matlab.ui.Figure/horzcat
While converting from 'function_handle':
The conversion returned a value of class 'matlab.mixin.Heterogeneous', which cannot be inserted into an array
of class 'matlab.ui.Figure'.
Error in matrix_input>submitbtn_Callback (line 63)
'Callback', [@testbtn_Callback, matrixdisplay]);
Error while evaluating DestroyedObject Callback
Any ideas one what's going wrong? I'm assuming I'm using the wrong Callback or something for the new button, but I can't seem to figure out what to fix/change.

Community Treasure Hunt

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

Start Hunting!

Translated by