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
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
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.
jayal
jayal 2016 年 10 月 7 日
編集済み: jayal 2016 年 10 月 7 日
Hi Walter, thank you for your answer. Yes, your assumption is correct. So I have the above code in one script file, including the callback functions (I also tried to have them in separate files, same result). Basically, I am watching the workspace tab inside octave and wait for a change/ that my variables appear. It looks like something is looping, so the CL prompt is in "busy" state, I can't interact. So far I only use the code from above (+ some clc, close/clear all etc.), no loops introduced yet. And yes, the changes to the workspace don't appear until I press ctrl+c, which seems to update everything, but of course this is not the intended functionality. (Also the printing to the command line is working, so when I press my button, the variables are displayed/outputted, when I remove the trailing ; ,but not saved to the workspace.)
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.
Which arguments would that be? And how to use them in the pushbutton uicontrol, like ('Callback', @Button1Callback(arg1, arg2) [...])? I don't get an error message even when I call my callback without any argument.
I naively just linked to the function I want to call when the button is pressed, which leads me to your next comment
(because you are not inside any callback after you return from the button1 callback)
So I guess, I need something like this? How to return, to a view of the figure with no interaction, like init state /wait for next pushbutton action or something.
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
jayal 2016 年 10 月 7 日
I am trying to read up a little bit more, as the suggested arguments didn't change anything.
Yes. Octave.. I really don't know why we are urged to use it here. Will try to find an octave forum which is as active and supportive as this Matlab Q&A.
Thanks again!

サインインしてコメントする。

回答 (0 件)

カテゴリ

質問済み:

2016 年 10 月 7 日

コメント済み:

2016 年 10 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by