ERROR: Error using {} Variable index exceeds table dimensions. How to apply function to all cells/tables?
Afficher commentaires plus anciens
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?
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 11 Juin 2024
first_table = results_velocity_diff{1, 1};
% Initialize a cell array to store the results
results = cell(1, width(first_table));
we deduce from this code that results_velocity_diff{1,1} yields a table object.
sub_cell_array = results_velocity_diff{i};
We deduce from the fact that that line worked, that results_velocity_diff is not a table itself -- if it was, then you would have gotten the complaint about 1 subscript there already. So results_velocity_diff is a cell array, and results_velocity_diff{i} is the same as results_velocity_diff{i,1} or results_velocity_diff{1,i} and so yields a table object. So sub_cell_array is a table object.
table_data = sub_cell_array{j};
And here you try to index the table object using a single subscript, which is not a permitted operation.
Possibly you want
sub_cell_array = results_velocity_diff(i, :);
Catégories
En savoir plus sur Tables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!