ERROR: Error using {} Variable index exceeds table dimensions. How to apply function to all cells/tables?
Hi, I have a fucntion called "edit" which I want to apply to all columns of all tables in all cells in "results_velocity_diff" (see attachment).
I tried it using this code as a test:
% Get the first table from the first cell
first_table = results_velocity_diff{1, 1};
% Initialize a cell array to store the results
results = cell(1, width(first_table));
% Apply edit to each column in the first table
for col = 1:width(first_table)
results{col} = edit(first_table{:, col}, 0, 0, 0);
end
That worked fine. But when I try and apply it to the rest of the columns in the tables of the cells using this code:
% Initialize results_edit to store the results
results_edit = {};
% Iterate through each cell array
for i = 1:numel(results_velocity_diff)
sub_cell_array = results_velocity_diff{i};
% Initialize a new sub cell array for the results
edit_sub_cell_array = {};
% Iterate through each table in the sub cell array
for j = 1:numel(sub_cell_array)
table_data = sub_cell_array{j};
edit_table = table(); % Initialize an empty table to store results
% Iterate through each column in the table
for col = 1:width(table_data)
% Apply the edit function to the current column
edit_result = edit(table_data{:, col}, 0, 0, 0);
% Store the result in the edit table
% Creating a new variable name dynamically
var_name = table_data.Properties.VariableNames{col};
edit_table.(var_name) = edit_result;
end
% Store the table in the sub cell array
edit_sub_cell_array{end+1} = edit_table;
end
% Store the sub cell array in results_edit
results_edit{end+1} = edit_sub_cell_array;
end
Then I get the error:
Error using {}
Subscripting into a table using one subscript (as in t(i)) is not supported. Specify a row subscript and a variable subscript, as in t(rows,vars). To select variables, use t(:,i) or for one
variable t.(i). To select rows, use t(i,:).
Error in
table_data = sub_cell_array{j};
What am I doing wrong?Hi, I have a fucntion called "edit" which I want to apply to all columns of all tables in all cells in "results_velocity_diff" (see attachment).
I tried it using this code as a test:
% Get the first table from the first cell
first_table = results_velocity_diff{1, 1};
% Initialize a cell array to store the results
results = cell(1, width(first_table));
% Apply edit to each column in the first table
for col = 1:width(first_table)
results{col} = edit(first_table{:, col}, 0, 0, 0);
end
That worked fine. But when I try and apply it to the rest of the columns in the tables of the cells using this code:
% Initialize results_edit to store the results
results_edit = {};
% Iterate through each cell array
for i = 1:numel(results_velocity_diff)
sub_cell_array = results_velocity_diff{i};
% Initialize a new sub cell array for the results
edit_sub_cell_array = {};
% Iterate through each table in the sub cell array
for j = 1:numel(sub_cell_array)
table_data = sub_cell_array{j};
edit_table = table(); % Initialize an empty table to store results
% Iterate through each column in the table
for col = 1:width(table_data)
% Apply the edit function to the current column
edit_result = edit(table_data{:, col}, 0, 0, 0);
% Store the result in the edit table
% Creating a new variable name dynamically
var_name = table_data.Properties.VariableNames{col};
edit_table.(var_name) = edit_result;
end
% Store the table in the sub cell array
edit_sub_cell_array{end+1} = edit_table;
end
% Store the sub cell array in results_edit
results_edit{end+1} = edit_sub_cell_array;
end
Then I get the error:
Error using {}
Subscripting into a table using one subscript (as in t(i)) is not supported. Specify a row subscript and a variable subscript, as in t(rows,vars). To select variables, use t(:,i) or for one
variable t.(i). To select rows, use t(i,:).
Error in
table_data = sub_cell_array{j};
What am I doing wrong? Hi, I have a fucntion called "edit" which I want to apply to all columns of all tables in all cells in "results_velocity_diff" (see attachment).
I tried it using this code as a test:
% Get the first table from the first cell
first_table = results_velocity_diff{1, 1};
% Initialize a cell array to store the results
results = cell(1, width(first_table));
% Apply edit to each column in the first table
for col = 1:width(first_table)
results{col} = edit(first_table{:, col}, 0, 0, 0);
end
That worked fine. But when I try and apply it to the rest of the columns in the tables of the cells using this code:
% Initialize results_edit to store the results
results_edit = {};
% Iterate through each cell array
for i = 1:numel(results_velocity_diff)
sub_cell_array = results_velocity_diff{i};
% Initialize a new sub cell array for the results
edit_sub_cell_array = {};
% Iterate through each table in the sub cell array
for j = 1:numel(sub_cell_array)
table_data = sub_cell_array{j};
edit_table = table(); % Initialize an empty table to store results
% Iterate through each column in the table
for col = 1:width(table_data)
% Apply the edit function to the current column
edit_result = edit(table_data{:, col}, 0, 0, 0);
% Store the result in the edit table
% Creating a new variable name dynamically
var_name = table_data.Properties.VariableNames{col};
edit_table.(var_name) = edit_result;
end
% Store the table in the sub cell array
edit_sub_cell_array{end+1} = edit_table;
end
% Store the sub cell array in results_edit
results_edit{end+1} = edit_sub_cell_array;
end
Then I get the error:
Error using {}
Subscripting into a table using one subscript (as in t(i)) is not supported. Specify a row subscript and a variable subscript, as in t(rows,vars). To select variables, use t(:,i) or for one
variable t.(i). To select rows, use t(i,:).
Error in
table_data = sub_cell_array{j};
What am I doing wrong? function, error, subscripting, table MATLAB Answers — New Questions