How do I update table in App Designer using multiselect with listbox?

6 次查看(过去 30 天)
Hi! I having trouble with app designer in MATLAB and couldn't seem to find a solution. Please do help me!
I have a table data containing 49 columns and 63 rows.
properties (Access = private)
x = readtable('Table.xlsx'); % table read from formatted excel file
end
Table do show up properly in app designer using following code
function startupFcn(app)
%loading data into table in app
app.UITable.Data = app.x;
% Change column name from imported data
app.UITable.ColumnName = app.x.Properties.VariableNames;
% make sure first column is fixed?
end
And the listbox contains all the column names.
% Create LocationsListBox
app.LocationsListBox = uilistbox(app.UIFigure);
app.LocationsListBox.Items = {'Location1','Location2','Location3','Location4','Location5','Location6','Location7','Location8','Location9','Location10','Location11','Location12','Location13','Location14','Location15','Location16','Location17','Location18','Location19','Location20','Location21','Location22','Location23','Location24','Location25','Location26','Location27','Location28','Location29','Location30','Location31','Location32','Location33','Location34','Location35','Location36','Location37','Location38','Location39','Location40','Location41','Location42','Location43','Location44','Location45','Location46','Location47','Location48'};
app.LocationsListBox.ItemsData = {'2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', ''};
app.LocationsListBox.ValueChangedFcn = createCallbackFcn(app, @LocationsListBoxValueChanged, true);
app.LocationsListBox.Position = [22 47 255 114];
app.LocationsListBox.Value = '2';
So I want to use listbox and multiselect locations and hence only the selected ones will be shown in the table like a filter function.
% Value changed function: LocationsListBox
function LocationsListBoxValueChanged(app, event)
app.LocationsListBox.Multiselect='on';
%enable multi select - hold shift or ctrl to select
%match location name to app.UITable first row - location names
selectedItems = app.LocationsListBox.ItemsData;
noselections=length(selectedItems); %number of selections
columnnamest=app.UITable.ColumnName;
we=app.UITable.Data;
for w=1:noselections
y=cell2mat(selectedItems(w));
y=str2double(y);
app.UITable.ColumnName(w)=columnnamest(y);
app.UITable.Data(:,w)=we(:,y);
end
if w<48 %deleteing excess
app.UITable.Data(:,w:48)=[]; %Error stateent: Matrix index is out of range for deletion.
app.UITable.ColumnName(:,w:48)=[];
end
end
Error statement:
Matrix index is out of range for deletion.
And also, the code generally does not select and update table.
Thank you for your help!

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by