ERROR: Error using {} Variable index exceeds table dimensions. How to apply function to all cells/tables?

8 vues (au cours des 30 derniers jours)
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?
  1 commentaire
lil brain
lil brain le 11 Juin 2024
EDIT:
I now fixed the code like this:
% 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:size(table_data, 2) % Using size instead of width for double array
% 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 = ['Column_' num2str(col)]; % Creating generic names for double array columns
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
But now I get a new error message which I cant solve:
Error using {}
Variable index exceeds table dimensions.
Error in
table_data = sub_cell_array{:,j};

Connectez-vous pour commenter.

Réponse acceptée

Voss
Voss le 11 Juin 2024
It looks like this code was written expecting results_velocity_diff to be a cell array of cell arrays of tables, but in fact results_velocity_diff is a cell array of tables.
load('results_velocity_diff')
results_velocity_diff % cell array of tables
results_velocity_diff = 3x12 cell array
Columns 1 through 10 {2154x2 table} {2154x2 table} {2154x2 table} {2153x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} Columns 11 through 12 {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table}
So you basically can remove the 2nd loop (the j loop).
% Initialize results_edit to store the results
results_edit = cell(size(results_velocity_diff));
% Iterate through each cell
for i = 1:numel(results_velocity_diff)
table_data = results_velocity_diff{i};
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 results_edit
results_edit{i} = edit_table;
end
results_edit
results_edit = 3x12 cell array
Columns 1 through 10 {2154x2 table} {2154x2 table} {2154x2 table} {2153x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} Columns 11 through 12 {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table}
Since I don't have your edit function, I made a simple one to use here:
function out = edit(varargin)
% dummy function for demonstration, just returns the first input
out = varargin{1};
end
But really you should avoid naming a function "edit" because that's the name of an important built-in MATLAB function.
  1 commentaire
lil brain
lil brain le 13 Juin 2024
Great stuff again @Voss. Works well! And yes the edit function name is subideal thats very true. Will keep that in mind.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
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 Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by