MATLAB Answers

How can I run a previously used function using a pushbutton?

2 views (last 30 days)
Chris Gnam
Chris Gnam on 7 Feb 2016
Answered: Chris Gnam on 7 Feb 2016
So I'm writing a code that will ask a user to input a matrix. It will then show them the matrix in a table, and ask them if it is correct. There will be a 'Yes' and a 'No' button. If they click the 'Yes' button, it will simply close the program. If they click the 'No' button, I would like it to go back and run the original function again, which asks them to input a matrix. I'm very confused, as I can't seem to figure out how you're supposed to do this, but it feels like this should be fairly easy... How can I make a pushbutton initiate a function which has already been used, without rewriting the entire function again? I can include my entire code if necesary, but it is rather lengthy, and all of it works properly except for the 'No' button. Basically my question comes down to this: Assuming that I write a rather lengthy function:
function matrix_input %Generate the actual window prompt
~ ~ ~ ~ ~ Lots of stuff going on in here including creating a window to input the matrix, and a 'submit' button
~ ~ ~ ~ ~
Which then opens up a second gui which displays the matrix and has the 'yes' and 'no' buttons:
function submitbtn_Callback
~ ~ ~ ~ Lots of stuff going on in here, including the creation of a 'matrixdisplay' window which has the two buttons on it
~ ~ ~ ~
How can I, later on, call back this function without needing to rewrite it all? I don't have any idea how to write this, but my best guess was something like this:
function nobtn_Callback(~, ~, matrixdisplay)
function matrix_input
In English, it'd be something like:
When nobtn is pressed, go to the function 'matrix_input' and do it again
I'm super lost, I feel like this should be relatively easy, so any help would be greatly appreciated!
  1 Comment
Geoff Hayes
Geoff Hayes on 7 Feb 2016
Chris - are you using GUIDE to create your GUI(s)? Under what condition does matrix_input launch the second GUI? Finally, can't the nobtn_Callback just call the main program, matrix_input again (and close itself)?

Sign in to comment.

Answers (1)

Chris Gnam
Chris Gnam on 7 Feb 2016
I figured it out! All I need to do is put:
function nobtn_Callback(~, ~, matrixdisplay)
run matrix_input
And it worked perfectly!

Community Treasure Hunt

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

Start Hunting!

Translated by