16 ビュー (過去 30 日間)
古いコメントを表示
lil brain 約18時間 前
コメント済み: lil brain 約17時間 前
- results_velocity_diff.mat
MATLAB Online で開く
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 件のコメント -1 件の古いコメントを表示-1 件の古いコメントを非表示
-1 件の古いコメントを表示-1 件の古いコメントを非表示
lil brain 約17時間 前
このコメントへの直接リンク
https://jp.mathworks.com/matlabcentral/answers/2127691-error-error-using-variable-index-exceeds-table-dimensions-how-to-apply-function-to-all-cells#comment_3184601
MATLAB Online で開く
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};
サインインしてコメントする。
サインインしてこの質問に回答する。
回答 (2 件)
Walter Roberson 約17時間 前
MATLAB Online で開く
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, :);
0 件のコメント -2 件の古いコメントを表示-2 件の古いコメントを非表示
-2 件の古いコメントを表示-2 件の古いコメントを非表示
サインインしてコメントする。
Voss 約17時間 前
MATLAB Online で開く
- results_velocity_diff.mat
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.
0 件のコメント -2 件の古いコメントを表示-2 件の古いコメントを非表示
-2 件の古いコメントを表示-2 件の古いコメントを非表示
サインインしてコメントする。
サインインしてこの質問に回答する。
参考
カテゴリ
MATLABLanguage FundamentalsData TypesTables
Help Center および File Exchange で Tables についてさらに検索
タグ
- function
- error
- subscripting
- table
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
最寄りの営業オフィスへのお問い合わせ