how to select specific columns or rows of a table in appdesigner of matlab?

1.1k Views Asked by At

I Want to select a row or column of a table with the edit field component and do some actions on these data and show the result in the first cell of table ([1,1]) rowNames={1:100} columnName={A:ZZ} like this: sum(A1:A20) or Max(AA5:AA10) I want to write above order in the edit field component and show the result of them in cell[A,1] How can I do that?

1

There are 1 best solutions below

8
On

Here's an implementation that might be similar to what you are attempting to achieve.

Any subset can be calculated using the command such as: Sum(A1:A2)U(B2:B3)

Where A indicates the columns apart of the set and B indicates the rows apart of the set.

Some more test functions include: Sum(A1:A4) and Sum(B1:B4)

Excel style table calculator

%Random data%
global Data;
Data = [1 2 3 4; 1 2 3 4; 1 2 3 4; 1 2 3 4; 1 2 3 4];

%Converting data into table%
Data = array2table(Data);

%Grabbing the size of the data%
[Number_Of_Rows, Number_Of_Columns] = size(Data);

%Creating arrays for setting the row and column names%
Row_Labels = strings(1,Number_Of_Rows);
Column_Labels = strings(1,Number_Of_Columns);

for Row_Scanner = 1: +1: Number_Of_Rows
Row_Labels(1,Row_Scanner) =  ["B" +  num2str(Row_Scanner)];
end

for Column_Scanner = 1: +1: Number_Of_Columns
Column_Labels(1,Column_Scanner) = ["A" + num2str(Column_Scanner)];
end


Row_Labels = cellstr(Row_Labels);
Column_Labels = cellstr(Column_Labels);


%UItable%
Main_Figure = uifigure;
Table = uitable(Main_Figure,'Data',Data);
Table.ColumnName = Column_Labels;
Table.RowName = Row_Labels;
set(Table,'ColumnEditable',true(1,Number_Of_Columns))

%Callback function to update the table%
% Table.CellEditCallback = @(Table,event) Update_Table_Data(Table);


%UIeditfield%
Selection_Field = uieditfield(Main_Figure,'text');
Field_Height = 20;
Field_Width = 100; 
X_Position = 350;
Y_Position = 200;
Selection_Field.Position = [X_Position Y_Position Field_Width Field_Height]; 


Result_Label = uilabel(Main_Figure);
Result_Label.Position = [X_Position Y_Position-100 Field_Width Field_Height]; 

Selection_Field.ValueChangedFcn = @(Selection_Field,event) Compute_Value(Table,Selection_Field,Result_Label);

%Computing value
function [Data] = Compute_Value(Table,Selection_Field,Result_Label)
Data = Table.Data;
User_Input_Function = string(Selection_Field.Value);

Function = extractBefore(User_Input_Function,"(");
% fprintf("Function: %s \n",Function);

Key_Pairs = extractBetween(User_Input_Function,"(",")");
% fprintf("Key Pairs: (%s)\n", Key_Pairs);

Key_1 = extractBefore(Key_Pairs(1,1),":");
Key_2 = extractAfter(Key_Pairs(1,1),":");
Key_1
Key_2

if length(Key_Pairs) == 2
Key_3 = extractBefore(Key_Pairs(2,1),":");
Key_4 = extractAfter(Key_Pairs(2,1),":");
Key_3
Key_4
end

%Exracting the letters of each key
if contains(Key_1, "A") == 1
% fprintf("Function on columns\n")
Minimum_Column = str2num(extractAfter(Key_1,"A"));
Maximum_Column = str2num(extractAfter(Key_2,"A"));
Table_Subset = Data(1,Minimum_Column:Maximum_Column);
end

if contains(Key_1, "B") == 1
% fprintf("Function on rows\n")
Minimum_Row = str2num(extractAfter(Key_1,"B"));
Maximum_Row = str2num(extractAfter(Key_2,"B"));
Table_Subset = Data(Minimum_Row:Maximum_Row,1);

end


if length(Key_Pairs) == 2
Minimum_Column = str2num(extractAfter(Key_1,"A"));
Maximum_Column = str2num(extractAfter(Key_2,"A"));
Minimum_Row = str2num(extractAfter(Key_3,"B"));
Maximum_Row = str2num(extractAfter(Key_4,"B"));
Table_Subset = Data(Minimum_Row:Maximum_Row,Minimum_Column:Maximum_Column);
end


Table_Subset = table2array(Table_Subset);



%Statements for each function%
if (Function == 'Sum' || Function == 'sum')
fprintf("Computing sum\n");
Result_Sum = sum(Table_Subset,'all');
Result_Sum
Result_Label.Text = "Result: " + num2str(Result_Sum);

end

if (Function == 'Max' || Function == 'max')
fprintf("Computing maximum\n");
Result_Max = max(Table_Subset);
Result_Max
Result_Label.Text = "Result: " + num2str(Result_Max);
end

if (Function == 'Min' || Function == 'min')
fprintf("Computing minimum\n");
Result_Min = min(Table_Subset);
Result_Min
Result_Label.Text = "Result: " + num2str(Result_Min);
end

end