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

5 views (last 30 days)
Faizan Poran
Faizan Poran on 19 Jan 2022
Commented: Rik on 29 Jan 2022
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.

Answers (1)

Rik
Rik on 20 Jan 2022
Edited: Rik on 20 Jan 2022
If you add uiwait before the nested function, this will pause execution of the main function. This will give the user time to click the button. Since clicking the button closes the figure, uiresume is called implicitly there.
For general advice and examples for how to create a GUI, have look at this thread.
The reason your current setup doesn't work is that your function has already returned before the callback has had a chance to change the value.
  1 Comment
Rik
Rik on 29 Jan 2022
Did my answer solve the problem? If so, please consider marking it as accepted answer. If not, feel free to post a comment with your remaining issues.

Sign in to comment.

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!