Octave/Matlab variable output from nested function not showing up without ctrl+c
古いコメントを表示
Hello, I am getting more and more confused about variable output, workspaces, variable passing to functions and so on. Please see my example I am working on a minimal GUI-like look and feel with Octave (University, no money..). I am quite satisfied with the look, as this will be all I need for now. However this thing lacks functionality. Whenever I try to run a function via the pushbutton callback I am not able to retrieve any variable or other output from the callback function. I already tried with command assignin (in base or caller), and that seems to help a little. But, they just won't show up. To let the variables appear in the workspace I need to send an interrupt with cntrl+C. Then all variables show up instantly. How can I make the variables show up when I really need them? Is this a Octave thing with the nested function, newbie messing up syntax, or buggy because of using uicontrols?
Thanks for helping out. -Jay
%%Button functions / callbacks
function Button1Callback(button1)
testvar1 = 33;
testvar2 = 66;
assignin('base', 'testvar1',testvar1)
assignin('base', 'testvar2',testvar2)
endfunction
%
function Button2Callback(button2)
disp('nooothing here')
return
endfunction
%
%%%create figure as main gui view
fig1 = figure();
set(fig1, 'units','normalized','position',[.28 -.15 .95 .95])
% create axes on fig
ax1 = axes("parent", fig1,'Position',[0.29 0.12 0.65 0.80]);
% create button panel on fig
pan1 = uipanel ("parent", fig1, "title", "Functions/Options", "position", [.025 .08 .2 .85]);
% create buttons
button1 = uicontrol ('Callback', @Button1Callback, "parent", pan1, "string", "Load *.pr data", "position",[20 550 200 40]);
button2 = uicontrol ('Callback', @Button2Callback, "parent", pan1, "string", "Dummy", "position",[20 500 200 40]);
button3 = uicontrol ("parent", pan1, "string", "Button No. 2", "position",[20 450 200 40]);
button4 = uicontrol ("parent", pan1, "string", "Button No. 3", "position",[20 400 200 40]);
button5 = uicontrol ("parent", pan1, "string", "Button No. 4", "position",[20 350 200 40]);
button6 = uicontrol ("parent", pan1, "string", "Button No. 5", "position",[20 300 200 40]);
button7 = uicontrol ("parent", pan1, "string", "Another Button", "position",[20 150 200 40]);
% create an edit control
edit1 = uicontrol ("parent", pan1, "style", "edit", "string", "Text box to edit", "position",[20 250 200 40]);
% create a checkbox
cbox1 = uicontrol ("parent", pan1, "style", "checkbox", "string", "This is a checkbox", "position",[20 200 150 40]);
% create a text
text1 = uicontrol ("parent", pan1, "style", "text", "string", "Text Text Text...", "position",[20 100 150 40]);
5 件のコメント
Walter Roberson
2016 年 10 月 7 日
To confirm, you have defined Button1Callback and Button2Callback, and you create a figure using the code starting from "%%% create figure as main gui view"? And you are sitting at the command prompt? And you click on button1, but there is no change to the base workspace, and you return to the command prompt (because you are not inside any callback after you return from the button1 callback)? And then at the command prompt, you have to press control-C before the changes to the base workspace are visible?
Or is there some other code that you have not shown that is modal or is looping or otherwise keeping the command window going, and you have to interrupt that with control C, returning to the command prompt, in order to see the changes to the base workspace?
What mechanism are you using to observe the base workpace to see if it has changed or not?
Walter Roberson
2016 年 10 月 7 日
In MATLAB, callback functions must support at least two arguments, and you would get an error if a callback defined with only one argument was triggered.
Walter Roberson
2016 年 10 月 7 日
Callbacks set up like
button1 = uicontrol ('Callback', @Button1Callback, "parent", pan1, "string", "Load *.pr data", "position",[20 550 200 40]);
are fine. But you need
function Button1Callback(button1, event)
"Notice that the function handle does not explicitly refer to any input arguments, but the function declaration includes two input arguments. These two input arguments are required for all callbacks you specify as a function handle. MATLAB passes these arguments automatically when the callback executes. The first argument is the UI component that triggered the callback. The second argument provides event data to the callback function. If there is no event data available to the callback function, then MATLAB passes the second input argument as an empty array."
Your code is Octave code, not MATLAB code (MATLAB never uses "endfunction"), so the behaviour in Octave might be different, but this is a MATLAB forum and we advise you on what needs to be done in MATLAB.
jayal
2016 年 10 月 7 日
回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Octave についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!