Octave/Matlab variable output from nested function not showing up without ctrl+c

8 Ansichten (letzte 30 Tage)
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 Kommentare
Walter Roberson
Walter Roberson am 7 Okt. 2016
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 am 7 Okt. 2016
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!

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by