0
votes

I have a function which can be run in various different ways, depending on the state of 42 constants which are set at the top of the code. Up until now, if I want to run my function under different conditions, I simply open the MATLAB code and adjust the constant at the beginning of the code.

Fourty-two constants is a lot to keep track of, and I've found myself running experiments with certain switches accidentally left turned on. The obvious solution is to build a GUI, where my inputs can be seen visually in one place. The GUI would simply be a parameter setting window, with a big 'Go' button at the bottom which takes the inputs (all 42 of them!) and passes them into my main function.

I have come across GUIDE, which I have used to build a nice GUI. I have managed to get a button to launch my function, but I'm struggling to get the actual variables entered into text boxes and check box states to pass to the main function.

I understand it has something to do with 'Callbacks', but the documentation is unclear and seems to be more concerned with building GUIs in which the variables adjust the content of the current window.

As a basic example, I'm working with a check box. I know that when I click a check box, function checkbox1_Callback(hObject, eventdata, handles) executes. I have modified this function so that it returns a variable 'state', which is set during the function in the following way:

state = get(hObject,'Value')

This pops up a message saying that state has been changed, whenever I click the check box. Of course, as soon as this has happened, the function has ended and the variable destroyed. There doesn't seem to be any way of receiving the variable elsewhere. The .m code doesn't include a call to the checkbox1_Callback function anywhere, so I don't know where I can receive the input.

I had hoped I could just call the checkbox1_Callback function at the time of clicking the 'Go' button, but I don't know what arguments to pass to the callback.

Clearly I'm missing something fundamental here, but the documentation doesn't make this easy! Any pointers would be appreciated.

2

2 Answers

1
votes

You will probably only need one callback - on the "GO" button.

It sounds like you've already sorted this out - so you probably have a function like:

function go_Callback(hObject, eventdata, handles)

which gets executed when you press the "Go" button. If you don't have it, create it from GUIDE by right-clicking the "Go" button and selecting "View Callbacks"->"Callback".

From here, you can 'pull' the data from other GUI components. For example, if you have a text box called "threshold":

threshold = get(handles.threshold, 'String');

Similarly, for a checkbox:

checked = get(handles.my_checkbox, 'Value');
0
votes

I did not use GUIDE, so I can not answer your question straight away. But, I would consider programing the start screen 'by hand' in a normal MATLAB function. You can then declare all the ui components inside the function that shows the GUI. The callback for the GO button is declared as a local function, hence it has access to all the ui controls. When GO is pressed, you simply get the states of the uicontrols and run your function.

function setup_screen

init_figure = 1;
h_fig =   figure(...
    'BackingStore', 'on',...
    'DoubleBuffer','on',...
    'Render', 'zbuffer',...
    'Name', 'TecMod - Process Manager',...
    'NumberTitle','off',...
    'MenuBar','none',...
    'DockControls', 'off',...
    'Toolbar','none',...
    'units', 'characters',...
    'Position',[10 10 30 20],...
    'Units','characters');

hp_config = uipanel(...
    'Title','Setup',...
    'units', 'characters',...
    'Position',[1 1 28 18]);

hu_info = uicontrol('parent', hp_config, 'style','pushbutton',...
    'units','characters',  ...
    'TooltipString', 'Run the function',...
    'tag', 'hu_info',...
    'String', 'GO',...
    'Position', [1 1 25 3],...
    'Callback', @buttonCallback);

hu_choice = uicontrol('parent', hp_config, 'Style','checkbox',...
        'units', 'characters',...
        'Position',[1 10 25 3],...
        'String', 'Checkbox1',...
        'Value', 1);

    function buttonCallback(src,evt)
        if src==hu_info
            display('event from GO button');
            % get ui controls states
            display(['checkbox state ' num2str(get(hu_choice, 'Value'))]);
            % call your function with chosen parameters
        end
    end
end