1

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?

Learner
  • 39
  • 6

1 Answers1

0

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
MichaelTr7
  • 4,737
  • 2
  • 6
  • 21
  • thanks for your response, in case sum(A1B1: A2B4) it doesn't work correctly and other problem that I have, is in order Sum(AC50: AF60) I don't know how to get data from table. – Learner Sep 11 '20 at 14:19
  • @Learner I used a union approach for case Sum(A1B1:A2B4) -> Sum(A1:A2)U(B1:B4). If you would like to change the functionality parsing the strings in the field using extractBefore, extractAfter and extractBetween will help. – MichaelTr7 Sep 11 '20 at 14:23
  • @Learner With a few if statements and parsing the reverse case can be achieved. Let me know if you’d need any help with small bits of the implementation. – MichaelTr7 Sep 11 '20 at 14:26
  • @Learner is it possible for you to alternatively upload the code to GitHub? – MichaelTr7 Sep 11 '20 at 16:24
  • Sorry i dont have an access to GitHub ,is it imposible for you to receive it in email? – Learner Sep 11 '20 at 16:37
  • @Learner Yeah, here it is. Email: michaeltr7stackoverflow@gmail.com – MichaelTr7 Sep 11 '20 at 16:41
  • @Learner Sorry I just had the time to check to the code. I saw you opted for using the app designer. I'm not too familiar with using it since I mainly do things programmatically. – MichaelTr7 Sep 13 '20 at 00:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/221402/discussion-between-michaeltr7-and-learner). – MichaelTr7 Sep 13 '20 at 00:30