조회 수: 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://kr.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!
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
미주
- 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 (한국어)
지역별 지사에 문의