
add a number of rows corresponding to the number in a numeric box
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Image Analyst
2025-5-12
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:

另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Directed Graphs 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!