MATLAB: Function inside MATLAB Gui code not outputing variable

function outputfunctionsguihomeworksudoku

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(~,~)
ir=1:9;
ic=reshape(ir,3,3)';
numbers=SudokuGame(ir,ic);
to_be_removed=25;
numbers_removed=removed(numbers,to_be_removed);
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
end
...more code
function numbers=SudokuGame(ir,ic)
numbers=1+mod(bsxfun(@plus,ir,ic(:)),9);
%step 1
p=randperm(9,9);
%step 2 (rows)
r=bsxfun(@plus,randperm(3,3),3*(randperm(3,3)-1)');
%step 3 (cols)
c=bsxfun(@plus,randperm(3,3),3*(randperm(3,3)-1)');
%permute away
numbers=p(numbers);
numbers=numbers(r,:);
numbers=numbers(:,c);
%step 4 (transpose at random)
if randi(2)==1;
numbers=numbers';
end
end
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.

Best Answer

Morgan,
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);
removed=0;
while removed ~= to_be_removed;
for r=randi(9);
for c=randi(9);
if numbers(r,c) > 0;
numbers(r,c)=0;
removed=removed+1;
else
numbers(r,c)=numbers(r,c);
end
end
end
end
end
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!