Remove columns from a matrix with a loop.

3 次查看(过去 30 天)
Hi, I know I posted this a couple of days ago, and the info really helped, this is just a continuation of the question with my code posted.
I am graphing data from different databases, and sometimes the data is missing. In this case, instead of a double array, the data is a string 'No Data'. I am writing a loop to delete columns with this string in place of the data. I need to delete the column from the dataarray that holds the data, and the namearray that holds the names of the data. They are both 1 by n cell arrays.
What I have now:
for i=1:length(dataarray) %Length of the data array
if strcmp(dataarray{i},'No Data')
%Sees if the column of the data array has no data
namearray(:,i)=[];
%Deletes the name for which there is no data
dataarray(:,i)=[];
%Deletes the column that has no data
end
end
Now this works like a charm when there is one column that has no data. However, it fails when there is more than one.
I get: ??? Index exceeds matrix dimensions.
Error in ==>>
If strcmp(dataarray{i},'No Data'}
I am not really sure why I am getting this error. I know with loops each iteration moves the reference frame of the number deleted. (So like if the first column is deleted on the first repetition of the loop, now the second column becomes the first column).
Thanks for any help! I have been playing with this all morning, and I am not sure the best way to accomplish this is.

采纳的回答

Sean de Wolski
Sean de Wolski 2011-6-8
Once you delete a column your matrix is smaller so the last entry (length(dataarray)) no longer exists.
The easiest remediation for this is to run it backwards!
It would still be best if you create an index list of all of the columns you need to remove and then do it all at once. E.g. something like:
idx = cellfun(@(x)strcmp(x,'No Data'),dataarray);
dataarray(:,idx) = [];

更多回答(2 个)

Matt Fig
Matt Fig 2011-6-8
Quoting myself (although the quotes really mess up a post!) from your last question:
The reason why you should not use a FOR loop for this is that you set the loop to iterate over the original length of the variable. Then if you delete a value in the variable, the length of the variable decreases, but the loop will keep right on going.
Did you read this? Did you try the example I gave and think about the problem???
Use CELLFUN.
DA = {'No Data',magic(3),'No Data',[1 2 3],[6 6 6 6],'No Data'};
idx = cellfun(@(x) strcmp(x,'No Data'),DA);
DA = DA(~idx)
  2 个评论
daniel.x16
daniel.x16 2011-6-8
I thought that was what you recomeneded. Instead of going from 1:n, I went from 1:length(array).
daniel.x16
daniel.x16 2011-6-8
Nevermind...I read it through more carefully. That makes sense.

请先登录,再进行评论。


Fangjun Jiang
Fangjun Jiang 2011-6-8
You ignored the caution both Matt and I gave you in your previous question. You should not do it in the loop.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by