Passing String from Cell Array to Matrix

The following code is producing the errors:
Matrix index is out of range for deletion.
names{s}(vec_missing(q)-q+1,:)=[];
The strings in the array are the names of four matrices (as an example). The vector vec_missing contains the integer values of the rows in the matrices I want to delete.
However, I have no idea why passing the string from the cell array is not working. I've tried searching the forum, but any help is appreciated:
names = {'Mary','Mark','Luke','John'};
for q=1:length(vec_missing);
for s=1:numel(names)
names{s}(vec_missing(q)-q+1,:)=[];
end
end

댓글 수: 4

dpb
dpb 2014년 8월 12일
HINT: What happens to length() of the array when something is deleted from it? It's a very common newbie mistake -- working from the front to the end when deleting and thereby changing the location...
David Young
David Young 2014년 8월 12일
I suspect the problem is that you are trying to use the strings in names as if they were variable names. But you can't do this without calling the eval function. I'm not offering that as an answer, though, because it would be a bad solution. Better would be to store your arrays in a cell array, rather than their names.
Charlie
Charlie 2014년 8월 15일
편집: Charlie 2014년 8월 15일
DPB - If you look carefully, you can see that I have corrected for this by subtracting the value of the loop iteration (q) (and actually has nothing to do with the question which I asked, anyway).
David Young - Thank you for your very useful advice.
dpb
dpb 2014년 8월 15일
I wasn't dreaming you really intended that the names were intended to be the names of the arrays you intended the loop to act on. I'm w/ David Y; this is a bad idea and as the FAQ says, "don't do that!!!"

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

답변 (1개)

Gitesh Nandre
Gitesh Nandre 2014년 8월 19일

0 개 추천

I agree with David Young and dpb. You are using strings from 'names' cell array as names of variables for matrices. This is not possible without using 'eval' function. However, it is not recommended. As David suggested, here is a solution which makes use of cell array for storing matrices.
Mary = magic(3); %Creating sample matrices of dimension 3x3
Mark = magic(3);
Luke = magic(3);
John = magic(3);
vec_missing = [1 3]; % Need to delete fist and third row from each matrix
names = {Mary Mark Luke John}; %Storing above matrices in cell array
for i = 1:numel(names)
names{i}(vec_missing,:) = [];
end

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

질문:

2014년 8월 12일

답변:

2014년 8월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by