
add a number of rows corresponding to the number in a numeric box
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I want to make it so the number I set as the number of objects is the amount of rows created in the table after I press the set up objects button. However whenever I try to do this nothing is added to the table.
function SetupButtonPushed(app, event)
n = app.NumObjectsEditField.Value;
if n <= 0 || isnan(n)
uialert(app.UIFigure, 'Please enter a valid number of objects.', 'Invalid Input');
return;
end
app.NumObjects = n;
dataMatrix = num2cell(zeros(n, 3));
app.UITable.Data = dataMatrix;
app.UITable.ColumnName = {'Mass', 'X', 'Y'};
app.UITable.ColumnEditable = [true, true, true];
end
0 commentaires
Réponses (1)
Image Analyst
le 12 Mai 2025
The code basically works. You just need to be sure that the edit field is a numerical edit field, not a character edit field, and that you have a global property variable for NumObjects. Here is the code:
properties (Access = private)
NumObjects % Description
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: SetupButton
function SetupButtonPushed(app, event)
n = app.NumObjectsEditField.Value;
if n <= 0 || isnan(n)
uialert(app.UIFigure, 'Please enter a valid number of objects.', 'Invalid Input');
return;
end
app.NumObjects = n;
dataMatrix = num2cell(zeros(n, 3));
app.UITable.Data = dataMatrix;
app.UITable.ColumnName = {'Mass', 'X', 'Y'};
app.UITable.ColumnEditable = [true, true, true];
% Right now the text column headers are left aligned and the numbers are right aligned.
% Make them all center aligned.
s = uistyle('HorizontalAlignment', 'center');
% addStyle(app.UITable, s, 'table', '');
addStyle(app.UITable, s);
end
end
and I'm attaching a small demo .mlapp file that produces this window:

1 commentaire
Voir également
Catégories
En savoir plus sur Directed Graphs 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!