필터 지우기
필터 지우기

Error "Matrix index is out of range for deletion" with dynamic variable names but not hardcoded names?

조회 수: 1 (최근 30일)
I get the error for the following case using dynamic variables:
% some things I pulled out of my data for the example:
arms = {'L','R'};
fields.L = [17 8 12 16];
fields.R = [12,6];
[~,removeL,removeR] = intersect(fields.L,fields.R);
uniquefields = fields;
% loop through arms (errors here)
for iArm = 1:length(arms)
uniquefields.(arms{iArm})(['remove',arms{iArm}]) = [];
end
But it doesn't happen when I hardcode it:
uniquefields.L(removeL) = [];
uniquefields.R(removeR) = [];
Does anyone know how to fix it? Thanks for any help!

채택된 답변

Philip Borghesani
Philip Borghesani 2016년 1월 29일
The code blocks are not doing the same thing. Your loop is doing:
uniquefields.('L')('removeL') = []
It could be fixed with an eval of the string but that is going in the wrong direction. I suggest trying this sequence:
arms = {'L','R'};
fields.L = [17 8 12 16];
fields.R = [12,6];
[~,remove{1},remove{2}] = intersect(fields.L,fields.R);
uniquefields = fields;
% loop through arms
for iArm = 1:length(arms)
uniquefields.(arms{iArm})(remove{iArm}) = [];
end
There are probably much better solutions for your final code I don't much like needing a list of field names to be used dynamically.
  댓글 수: 1
aacarey
aacarey 2016년 1월 29일
Thank you! I didn't realize that my variable was just staying as a string. This is part of a larger structure with a lot of data. I like keeping my data in large branching structures because the organization of fields is more understandable to me than keeping separate arrays. The fields become kind of "human readable" in the end.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by