add a number of rows corresponding to the number in a numeric box

4 ビュー (過去 30 日間)
Owen
Owen 2025 年 5 月 12 日
コメント済み: Image Analyst 2025 年 5 月 13 日
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

回答 (1 件)

Image Analyst
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:
  1 件のコメント
Image Analyst
Image Analyst 2025 年 5 月 13 日
Owen: did you try it yet? Please let me know.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDirected Graphs についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by