Function inside MATLAB Gui code not outputing variable
Ältere Kommentare anzeigen
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.
6 Kommentare
Geoff Hayes
am 19 Nov. 2016
Morgan - are you observing any errors when you try to call SudokuGame from within your GUI? If I use your above example and call easygame, then the call to SudokuGame does return something. You may need to post an example that exhibits the behaviour that you are describing especially that of the pushbutton. Can you post that code instead?
Morgan Clendennin
am 19 Nov. 2016
Nick Counts
am 19 Nov. 2016
Morgan,
I think we need to see the text of the error you are receiving. You can also attach the actual .m files to the question to make our debugging easier.
When I copy-paste your code and wrap it in a function to test, SudokuGame() returns an array to the callback function. See my attached .m file.
I commented out the displayed() and userlock() calls since we don't have that code available. And I made a dummy version of removed() to make sure that numbers can be passed to other functions.
I'm afraid without seeing your actual error (and probably your code) it's going to be tough to go any farther.
My intuition is that you are running into either a variable scope issue or possibly an overloaded function name issue.
Morgan Clendennin
am 19 Nov. 2016
Nick Counts
am 19 Nov. 2016
Hi, Morgan,
Not trying to beat a dead horse, but in the future we need all the red error text, not just a small part snipped out of it. This means line numbers, actual lines of code that generated the error, traceback to prior functions, etc. - everything that is in red color.
See my answer below, it turns out the error is not where you described :)
Image Analyst
am 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.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Sudoku finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


