How to delete rows from a matrix array ?

I have a 1x8 matrix named waterConductivityData. Each column has 5 rows.
For example, waterConductivityData{1,3} contains the following string data:
's'
'd'
'2012-03-26 00:00'
'2012-03-26 01:00'
'2012-03-26 02:00'
and waterConductivityData{1,7} contains the following double data:
14
0
327
329
330
I wrote the following code to go through {1,3} and delete the cells that do not contain date data. (in other words, delete cells one and two)
waterConductivityData{1,3}(cellfun('length',waterConductivityData{1,3})==1) = [];
the output deleted the first two rows which was exactly what I needed:
waterConductivityData{1,3} = '2012-03-26 00:00' '2012-03-26 01:00' '2012-03-26 02:00'
I want to delete the same rows (rows one and two) from waterConductivityData{1,7}
I tried:
waterConductivityData{1,7}(cellfun('length',waterConductivityData{1,3})==1) = [];
but this does not work.
Basically, whatever rows are deleted from waterConductivityData{1,3} I need to have delted from waterConductivityData{1,7} also.
Can someone help please?

回答(2 个)

t = ~cellfun('isempty',waterConductivityData);
d = waterConductivityData{3};
ii = cellfun(@(x)numel(x)> 8,regexp(d,'\d'));
out = cellfun(@(x)x(ii),waterConductivityData(t),'un',0)

2 个评论

Jan
Jan 2013-7-21
编辑:Jan 2013-7-21
"~isempty(x) & numel(x)> 8" looks redundant.
Hi Jan! I agree with you, corrected. Thanks.

请先登录,再进行评论。

I figured out what my problem was and it was a stupid issue.
Let me explain. This was the order of my code:
waterConductivityData{1,3}(cellfun('length',waterConductivityData{1,3})==1) = [];
waterConductivityData{1,7}(cellfun('length',waterConductivityData{1,3})==1) = [];
You can see that I was deleting the first two rows of waterConductivityData{1,3} then using that corrected data to try and delete the first two rows of waterConductivityData{1,7}.
But I already had corrected waterConductivityData{1,3} so It made no sense.
The solution was to switch my code:
waterConductivityData{1,7}(cellfun('length',waterConductivityData{1,3})==1) = [];
waterConductivityData{1,3}(cellfun('length',waterConductivityData{1,3})==1) = [];
I'm sure my explination was a little hard to understand.... but it works now.
I really appreciate your help! Thank you.

类别

帮助中心File Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by