The order of GUI elements

Hi. I’m quite new at this…

I have a small program with some EDIT elements and a few pushbuttons. I would like to keep the order according to my example: First S.FIRST, then S.pb_Test and then S.SECOND. If I debug S.SECOND exist at row 45 (tmp1). But when I press the button S.SECOND does not exist.

Can I create a program that have pushbuttons that are independent of the order? I.e that can use data in a struct that are defined later in the program?

Example:

close all
clear S 

graphics_toolkit qt

% INIT: 
S.fh = figure('units','pixels',...
              'position',[10 10 400 200],...
              'menubar','none',...
              'name','TEST',...
              'numbertitle','off',...
              'resize','on');
% Edit one ---------------------------------------------------------------------
S.FIRST = uicontrol('style','edit',...
                    'position',[20 100 70 30],...
                    'horizontalalignment', 'right',....
                    'string','200');
%% Edit two -------------------------------------------------------------------
%% Works                 
%S.SECOND = uicontrol('style','edit',...
%                    'position',[20 50 70 30],...
%                    'horizontalalignment', 'right',...
%                    'string','15');                   
% Button -----------------------------------------------------------------------
S.pb_Test = uicontrol('style', 'push',...
                          'string', 'Test:',...
                          'horizontalalignment', 'left',...
                          'position', [120 100 120 30],...
                          'callback', {@pb_Test,S});  
% Edit two ---------------------------------------------------------------------
% Doesn't Works                   
S.SECOND = uicontrol('style','edit',...
                    'position',[20 50 70 30],...
                    'horizontalalignment', 'right',...
                    'string','15');
% Button -----------------------------------------------------------------------
function [] = pb_Test(varargin)
  S = varargin{3}; % Get the structure
  tmp = str2double(get(S.SECOND, 'string'));
  set(S.FIRST, 'string', num2str(tmp));  
endfunction
% END BUTTON -------------------------------------------------------------------
% A place for a breakpoint to find out if S.SECOND exist
tmp1 = str2double(get(S.SECOND, 'string')); 
set (gcf, "color", get(0, "defaultuicontrolbackgroundcolor"))
% Store the handle to the figure to 'S'
guidata(gcf, S)
% END

My system

  • OS: Windows 10 (21H2)
  • Octave version: Version 6.3.0**

If you’d like to track changes of your structure S, you could get its current value with guidata at the beginning of your callback function pb_Test.
You don’t need to pass it as part of the callback property. That’ll make a copy of S at the time the uicontrol graphics object is created.

2 Likes

Hi. Now I better understand why the program did not work the way I wanted it to, and how I now can get around my problem by using “guidata”! Thanks for the help.