Function inside MATLAB Gui code not outputing variable

4 views (last 30 days)
I am writing a GUI in matlab programmatically. I have all the code that I need for the window and the push buttons in the function for the GUI. For one of my pushbuttons, I have a function that references another function for the output of that function to be used in this function. However, the function that it needs to get the variable from is not outputting any data. the code works for that function if I try to run it outside of the GUI script meaning that I also have the function saved as a separate .M file and when I try to get it to output something alone from the GUI, I get an output. I'm hoping that someone can tell me what I'm maybe doing wrong? The following is an example of what I'm currently doing.
function easygame(~,~)
displayed(numbers_removed); %display the generated puzzle on the board for play by the user
userlock(numbers_removed); %locks the display from being altered except in the blank (removed) spaces
...more code
function numbers=SudokuGame(ir,ic)
%step 1
%step 2 (rows)
%step 3 (cols)
%permute away
%step 4 (transpose at random)
if randi(2)==1;
SudokuGame is supposed to output a number array that will be used in the easygame function but it is not for some reason. Because I also have SudokuGame saved as a separate function file, when I say numbers=SudokuGame(ir,ic) in the Matlab command window it runs fine. I also get the same error that numbers is not being generated if I try to reference the outside Matlab function in my GUI code instead of having it inside my GUI function code.
Image Analyst
Image Analyst on 20 Nov 2016
Morgan, I second what Nick says. We need ALL THE RED TEXT not just a small part snipped from it. We also would love it if you would read this link. Many people (like me for instance) won't even download and try your code until we see the entire error message so we know what line it crashed on. Come on, make it easy for us to help you, not hard. For example I see Nick downloaded it but he said "it turns out the error is not where you described", so that was kind of a waste of some of his time. He'd need to fix that before he could even get to the error you asked about.

Sign in to comment.

Accepted Answer

Nick Counts
Nick Counts on 19 Nov 2016
Edited: Nick Counts on 19 Nov 2016
Sorry if it seemed like we were asking you for something you already gave us, but the entirety of the error message is important to understand what's going on. In any case, this is what we were after:
Note that this error actually shows that the issue is in removed() and not in easygame().
Now that I see the code, it looks like it is a variable scope issue. Let me explain.
On line 114 you create a variable called numbers and assign it a value of what SudokuGame() returns. NOTE: this works. Now line 114 is inside the easygame() function block, so it is only available inside that function. This is referred to as variable scope (Matlab calls this workspaces).
On line 116, you create a variable numbers_removed and assign its value to what removed() returns. Here you have correctly passed the value of numbers to the removed() function. As I said above, the variable numbers is not available anywhere in your code except the easygame() function.
Your error actually happens on line 128, inside the removed() function:
function game=removed(numbers_removed,to_be_removed);
while removed ~= to_be_removed;
for r=randi(9);
for c=randi(9);
if numbers(r,c) > 0;
By your function definition, the first argument is assigned to a variable named numbers_removed, and the second to a variable named to_be_removed. You also define variables removed, r, and c in the function code. Everything is going great until you get to line 128, where you reference a variable called numbers. There is no variable with that name in the removed() workspace, so Matlab throws up its hands and gives up.
Note you have also defined your function removed() such that it will return a variable named game but you never create a variable called game inside the function.
These same errors are repeated in your other function displayed().
There were a few other minor bugs, like setting the 'BackGround Color' property instead of 'BackGroundColor'. I have attached a working version of your code - everything works except the solve button. I had to leave you a little fun ;)
Hopefully this made sense. Let us know if you have any more questions about where your variables are available. These links should be a good reference to look over:
Good luck!
Morgan Clendennin
Morgan Clendennin on 20 Nov 2016
Hi Nick, I have one other thing that I just thought of. If I want to include a hint button that will pick a random row and column and if there is no value currently in that position or the value doesn't equal the solution value for that cell, it will display the correct value for that cell. The function will output 1 correct hint for every press of the hint button. Is there a way to do this using the getCurrentGameBoard function we created earlier or is there even a simpler way to do it? Just looking for suggestions.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!