MATLAB GUI - how to make a button change a value then close window

563 views Asked by At

I am doing data processing from experiments, some data cannot be detected and given a 'type'. For this data I have made a GUI that plots the relevant data and allows the user to manually specify what data 'type' it is by means of 5 buttons.

The idea is this: if the data cannot be assigned a 'type' by the script a window opens asking for user input. Once the user presses the button indicating the 'type' a number is assigned/saved into the parent script and the window is closed.

Everything is working except saving the 'type' (integer as identifier) such that the parent script can read this. See below for a simplified version of what I have done so far:

xCur = 1:1000;
yCur = 1:1000;
i = 1;

[fig,currentExpType] = guiExpType(xCur,yCur);
waitfor(fig);
disp(['Experiment series ' num2str(i) ' is type ' num2str(currentExpType) ' (user input)']);


function [fig,expType] = guiExpType(xCur, yCur)

expType = [];

% prompt window
fig = uifigure;
fig.Name = "Determine Experiment Type";
fig.Position = [350 300 1200 675];

% determine prompt layout
gl = uigridlayout(fig,[9 2]);
gl.RowHeight = {40,30,40,40,40,40,40};
gl.ColumnWidth = {'1x',200};

% initiate modules in prompt
ttl = uilabel(gl,'Text','Unable to detect experiment type, please define with the buttons below.','FontSize',20,'FontWeight','bold');
lbl = uilabel(gl,'Text','Experiment type shown:');
axX = uiaxes(gl);
axY = uiaxes(gl);
btn1 = uibutton(gl,'Text','Positive x perturbation','ButtonPushedFcn',@(src,event)btn_callback(src,event,1));
btn2 = uibutton(gl,'Text','Negative y perturbation','ButtonPushedFcn',@(src,event)btn_callback(src,event,2));
btn3 = uibutton(gl,'Text','Negative x perturbation','ButtonPushedFcn',@(src,event)btn_callback(src,event,3));
btn4 = uibutton(gl,'Text','Positive y perturbation','ButtonPushedFcn',@(src,event)btn_callback(src,event,4));
btn5 = uibutton(gl,'Text','Undefined, skip this.','ButtonPushedFcn',@(src,event)btn_callback(src,event,5));

% layout of modules
% title
ttl.Layout.Row = 1;
ttl.Layout.Column = [1 2];
% label
lbl.Layout.Row = 2;
lbl.Layout.Column = 2;
% button expType1
btn1.Layout.Row = 3;
btn1.Layout.Column = 2;
% button expType2
btn2.Layout.Row = 4;
btn2.Layout.Column = 2;
% button expType3
btn3.Layout.Row = 5;
btn3.Layout.Column = 2;
% button expType4
btn4.Layout.Row = 6;
btn4.Layout.Column = 2;
% button undefined
btn5.Layout.Row = 7;
btn5.Layout.Column = 2;
% x plot
axX.Layout.Row = [2 4];
axX.Layout.Column = 1;
% y plot
axY.Layout.Row = [5 7];
axY.Layout.Column = 1;

% configure modules
axX.Title.String = 'Current Experiment Cursor x-data';
axY.Title.String = 'Current Experiment Cursor y-data';

% plot data on axes
plot(axX, xCur)
plot(axY, yCur)

% callback functions for buttons

    function btn_callback(src,event,newValue)
        expType = newValue;
        closereq();
    end
    
end

I have tried a whole bunch of different things but am losing inspiration, I don't think I fully understand the way the the nested function definition @(...,...)function(...,...) works. I do understand that in the callback I am defining a variable and then immediately closing the figure, I have tried using the set() and get() functions but haven't been successful.

Any help will be greatly appreciated.

1

There are 1 answers

9
whoknowsmerida On

This should give you an idea of how to implement it.

function  guiExpType

xCur = 1:1000;
yCur = 1:1000;

% prompt window
fig = uifigure;
fig.Name = "Determine Experiment Type";
fig.Position = [350 300 1200 675];

myhandles = guihandles(fig);
myhandles.expType = [];
myhandles.newType = [];

% determine prompt layout
gl = uigridlayout(fig,[9 2]);
gl.RowHeight = {40,30,40,40,40,40,40};
gl.ColumnWidth = {'1x',200};

% initiate modules in prompt
ttl = uilabel(gl,'Text','Unable to detect experiment type, please define with the buttons below.','FontSize',20,'FontWeight','bold');
lbl = uilabel(gl,'Text','Experiment type shown:');
axX = uiaxes(gl);
axY = uiaxes(gl);
btn1 = uibutton(gl,'Text','Positive x perturbation','ButtonPushedFcn',@(src,event)btn_callback(src,event,1));
btn2 = uibutton(gl,'Text','Negative y 2 perturbation','ButtonPushedFcn',@(src,event)btn_callback_2(src,event,312));
btn3 = uibutton(gl,'Text','Negative x 2 perturbation','ButtonPushedFcn',@(src,event)btn_callback_3(src,event,3));
btn4 = uibutton(gl,'Text','Positive y perturbation','ButtonPushedFcn',@(src,event)btn_callback(src,event,4));
btn5 = uibutton(gl,'Text','Undefined, skip this.','ButtonPushedFcn',@(src,event)btn_callback(src,event,5));

% dataType = myhandles.expType;
% dataType = myhandles.newType;

% layout of modules
% title
ttl.Layout.Row = 1;
ttl.Layout.Column = [1 2];
% label
lbl.Layout.Row = 2;
lbl.Layout.Column = 2;
% button expType1
btn1.Layout.Row = 3;
btn1.Layout.Column = 2;
% button expType2
btn2.Layout.Row = 4;
btn2.Layout.Column = 2;
% button expType3
btn3.Layout.Row = 5;
btn3.Layout.Column = 2;
% button expType4
btn4.Layout.Row = 6;
btn4.Layout.Column = 2;
% button undefined
btn5.Layout.Row = 7;
btn5.Layout.Column = 2;
% x plot
axX.Layout.Row = [2 4];
axX.Layout.Column = 1;
% y plot
axY.Layout.Row = [5 7];
axY.Layout.Column = 1;

% configure modules
axX.Title.String = 'Current Experiment Cursor x-data';
axY.Title.String = 'Current Experiment Cursor y-data';

% plot data on axes
plot(axX, xCur)
plot(axY, yCur)

% callback functions for buttons

    function btn_callback(src,event,newValue)
        
        myhandles = guidata(gcbo);
        myhandles.expType = newValue;
        guidata(gcbo,myhandles)
        
        closereq();
    end
    
    function btn_callback_2(src,event,newValue)
        
        myhandles = guidata(gcbo);
        myhandles.newType = newValue;
        fprintf('Experiment series value is %d\n',newValue);
        guidata(gcbo,myhandles) 
        
%         closereq();
    end


    function btn_callback_3(src,event,newValue)
        
        myhandles = guidata(gcbo);
        oldValue = myhandles.newType;
        fprintf('Test Experiment series value is %d\n',oldValue);
        guidata(gcbo,myhandles)
        
        
        closereq();
    end

end