Changing the Number of Rows in imported Table via Edit-Field in App Designer

2 次查看(过去 30 天)
in Matlab App Designer, I'm building an App which imports a .csv.
I've added a Numeric Edit Field in which is displayed the Number of Rows of the Imported Table.
app.RowsEditField.Value = size(ImportedTable,1);
I would like to make this Edit Field interactive, so that whatever Number I type into that EditField, the Imported Table will be modified to have that number of rows.
If I change the Value to one smaller than the displayed number of Rows, can I make this 'chop' off the corresponding number of Rows in the Table?
If the Imported Table has 100 Rows, and the EditField displays '100', and I change that to 80, I'd like the App to then chop off 20 Rows, let's say the 20 first Rows, of the imported Table.
When I enter a Number greater than the number of Rows in the imported Table, it should simply reset my input to the number of rows, e.g. when I type 250 into the EditField, it should simply jump straight back at displaying 100, not add 150 rows into the Table.
Thank you most kindly in advance!

回答(1 个)

Vedant Shah
Vedant Shah 2025-2-18
编辑:Vedant Shah 2025-2-18
To make the Numeric Edit Field interactive, we need to add a callback function. The desired functionality is as follows: if the value in the Numeric Edit Field is less than or equal to the total number of rows in the table, the table will be modified to display only the specified number of rows by removing the initial extra rows. If the specified number of rows exceeds the total number of rows in the table, the table will display all rows, and the Numeric Edit Field will update to reflect the total number of rows present in the table.
The following Implementation steps can be followed:
First, add two private properties to manage the imported data and the total number of rows:
properties (Access = private)
ImportedTable
TotalRows
end
Implement the“startupFcnto load the entire table when the app is launched:
function startupFcn(app)
% Read the CSV file into a table
app.ImportedTable = readtable("yourcsvfile.xlsx");
app.UITable.Data = app.ImportedTable;
app.TotalRows = size(app.ImportedTable, 1);
% Update the Numeric Edit Field with the number of rows
app.EditField.Value = size(app.ImportedTable, 1);
end
Then, add a callback function for the Numeric Edit Field to achieve the intended functionality:
function RowsEditFieldValueChanged(app, event)
newRowCount = app.EditField.Value;
% Check if the new row count is less than the current row count
if newRowCount < app.TotalRows
app.UITable.Data = app.ImportedTable((app.TotalRows - newRowCount + 1):end, :);
elseif newRowCount > app.TotalRows
app.EditField.Value = app.TotalRows;
app.UITable.Data = app.ImportedTable;
end
This makes the app work as intended. Below is a screenshot demonstrating the app's functionality:

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by