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.

1 vue (au cours des 30 derniers jours)
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.

Réponse acceptée

Benjamin Großmann
Benjamin Großmann le 3 Mai 2021
Modifié(e) : Benjamin Großmann le 3 Mai 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 commentaires
Benjamin Großmann
Benjamin Großmann le 4 Mai 2021
Modifié(e) : Benjamin Großmann le 4 Mai 2021
It depends on how your program looks like. Maybe an object oriented approach would be beneficial.
1) write a class that stores properties and methods for your task, the simplest form would be a class only containing the property "logicalMatrix" (please give this class a better name than I did)
% contents of MyClass.m
classdef MyClass < handle
properties
logicalMatrix
end
methods
function obj = MyClass()
end
end
end
2) Add a property "MyClass" as well as an App Input Argument "MyClass" to the app
3) Add a startup function to the app that takes the input and sets the corresponding property:
% Code that executes after component creation
function startupFcn(app, MyClass)
app.MyClass = MyClass;
end
4) Add a close request function that sets the table data to the property of the MyClass instance:
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
% get table data
data = app.UITable.Data;
% check if data is set (initially the data is empty)
if isempty(data) % if empty, write empty to logicalMatrix property and delete the app
app.baseClass.logicalMatrix = [];
delete(app)
else % if not empty, write data to logicalMatrix and delete the app
app.baseClass.logicalMatrix = table2array(data);
delete(app)
end
end
5) Write a program (e.g. script) that creates an instance of the class, calls the app and waits until the app is closed.
% Content of aScript.m
clearvars
close all
clc
% create instance of your handle class, one property is 'logicalMatrix'
myClassInstance = MyClass();
% start the app called "app1", pass the class instance and wait until the app is closed
uiwait(app1(myClassInstance).UIFigure)
% display the logical matrix
disp(myClassInstance.logicalMatrix)
Michael Cooley
Michael Cooley le 6 Mai 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!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Develop Apps Using App Designer dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by