
add a number of rows corresponding to the number in a numeric box
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Image Analyst
am 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 Kommentar
Siehe auch
Kategorien
Mehr zu Directed Graphs finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!