MATLAB Answers

Trying to build an app for user to define dimensions for a m*n grid that can tell the app to automatically make the dimensions of check boxes that were requested.

2 views (last 30 days)
Hello,
I am currently trying to learn how the app program works on matlab and I am trying to figure out how to create an app in which the user can first define the matrix size of checkboxes that will then be displayed for the user to check or be left blank. The purpose of doing so is to convert these checkmarks into a 0/1 format into a matrix into my already written code. Also how would i bring my code into the app? I have a picture attatched for further explanation and any help at all will be greatly appreicated.

Accepted Answer

Benjamin Großmann
Benjamin Großmann on 3 May 2021
Edited: Benjamin Großmann on 3 May 2021
In app designer add two "edit field (numeric)", name the first one editN and the second one editM. Next, add a button, named "Button". And finally add a Table named UITable. Delete all table columns.
Now we have an app that should look like this:
I think the easiest approach is to generate a table of size (num_row x num_col) with logical data type. If you add this table as data to an uitable, then the checkboxes are automatically added since the datatype is logical.
Add a ButtonPushed Callback to your button and paste the following code.
function ButtonPushed(app, event)
n = app.editN.Value; % number of rows
m = app.editM.Value; % number of columns
% create a table with size [n,m] and all logical values
t = table( ...
'Size', [n,m], ...
'VariableTypes', repmat("logical", 1, m) ...
);
% add the table t as data to the uitable
app.UITable.Data =t;
% set the columns editable property to true
app.UITable.ColumnEditable = true;
% set the row and column name to 1 ... n ; 1 ... m
app.UITable.ColumnName = string(1:m);
app.UITable.RowName = string(1:n);
end
If you type in the size and push the button, checkboxes are added to the table:
You can access the data (check box status) pretty easy by app.UITable.Data or convert it to an array (logical matrix) via
logicalMatrix = table2array(app.UITable.Data)
If you do not want to use a table, you could also add checkboxes programmatically, (e.g. inside a loop), but here you have to take care of positions by yourself:
for ii = 1:n
for jj = 1:m
cb(ii, jj) = uicheckbox(app.UIFigure, 'Position', aPosition{ii, jj})
end
end
  3 Comments
Michael Cooley
Michael Cooley on 6 May 2021
Thank you, I have been doing a lot of research on OOP due to your comment and it seems that you are right. It may take me a little bit of time to implement this correctly, but you have helped me out so much thank you!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by