
add a number of rows corresponding to the number in a numeric box
4 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Image Analyst
on 12 May 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 Comment
See Also
Categories
Find more on Directed Graphs in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!