How to supply arguments to a UI slider callback function?

I want to make a plot with a UI slider function to make the plot interactive. I initialise the slider with the following code

%% Add ui ‘slider’ element
low_clim_slider = uicontrol( ‘parent’ , f , …
‘style’, ‘slider’, …
‘Units’, ‘normalized’, …
‘position’, [0.03, 0.01, 0.3, 0.03], …
‘min’, 0, …
‘max’, 0.49, …
‘value’, 0, … ## initial setting for slider
‘callback’, { @posmap_clim_redraw } ) ;

which has the ‘callback’ function named “posmap_clim_redraw.” At the moment the arguments for this ‘callback’ function are supplied as global variables, but this is causing some problems as the data for these input arguments is constantly changing due to an automatic updating process via a crontab job, but global variables can only be initialised once. How can I rewrite the above so that the callback function can be explicitly provided the updated data without using global variables? I was hoping something along the lines of

‘callback’, { @posmap_clim_draw( arg1 , arg2 , arg3 , … ) } ) ;

might work, but I am unsure of the exact syntax to use.

I remember that callbacks can obtain and pass the values via get and set functions, I’m going to tweak your code a little to make an example:

f=figure('position',[200,300,640,480]);

Frame = axes ( ...
    'position', [0, 0.2, 1, 0.6], ... 
    'xtick',    [], ... 
    'ytick',    [], ...
    'xlim',     [0, 1], ... 
    'ylim',     [0, 1]);

surf(sombrero())
axis tight
colorbar


%% Add ui 'slider' element
low_clim_slider = uicontrol('parent' , f , ...
                            'style', 'slider', ...
                            'Units', 'normalized', ...
                            'position', [0.03, 0.01, 0.3, 0.03], ...
                            'min', 0, ...
                            'max', 0.49, ...
                            'value', 0, ... ## initial setting for slider
                            'SliderStep',[0.01, 0.1],...
                            'callback', { @posmap_clim_redraw } ) ;



function posmap_clim_redraw(hObject, eventdata, handles)

    value = get(hObject,'Value');
    set(gca,'clim',[value,value*2]);
    
endfunction

Gets you something like this:

window

Instead of using global variables, you could also use setappdata and getappdata to store and retrieve the data from the figure itself.

I’m not certain what you mean by “but global variables can only be initialised once” and that being an issue. IIUC, the value of global variables can be set any number of times.

I have managed to solve my problem by using the function “guidata” to attach a structure containing my plotting variables to the plot handle,

## Add ui 'slider' element for clim colour axis on colormap
clim_slider = uicontrol( 'parent' , f ,                ...
   'style', 'slider',                    ...
   'Units', 'normalized',                ...
   'position', [0.03 , 0.01, 0.3, 0.03], ...
   'min', 0,                             ...
   'max', 0.49,                          ...
   'value', 0,                           ... initial setting for slider
   'callback', { @posmap_clim_redraw } ) ;

plot_data_struct = struct( 'Arg1' , arg1_value , 'Arg2' , arg2_value , ... ) ;
guidata( f , plot_data_struct ) ; 

which can then be extracted within the function “posmap_clim_redraw”

## Extract data for plotting from hObject
plot_data = guidata( hObject ) ;
arg1_value = plot_data.Arg1 ;
arg2_value = plot_data.Arg2 ;
...

along with the slider value

clim_slider_val = get( hObject , 'value' ) ;

and then proceed to plot
anim