1
votes

I got a problem that i think i have already answered, but it didnt work. Here is the deal:

I have a large cell array (about 300000x60) with some numeric data, some dates, some blank, some strings that I have to filter (like in Excel): For example:

m = ...
{   'date '    'code' 'number'  'market'  'max'     'min'    
 '01/01/2000'   'tsa'    1          0      0.9       0.0008
 '01/01/2000'   'sje'    2          0      1.8       1.5
 '01/02/2000'   'koi'    1          1      5.5       1.8
 '02/01/2000'   'sjk'    2          0      5.8       3.5
 '05/02/2000'   'kkj'    5          7      5.5       3.8 };  

I can filter the strings ('code' column) with:

b = m(strcmp('tsa',m(:,2)),:);

and as result:

b =
'01/01/2000'   'tsa'    1          0      0.9       0.0008

(this is working perfectly).

BUT, when I tried to filter the numbers, with c=m([m{:,3}] == 1,:); I had some strange answers in "c" (I got a cell array with all possible values in column 3, not only the ones which correspond to number '1')!

I want the answer like:

    c = m([m{:,3}] == 1,:)
    c =
    '01/01/2000'   'tsa'    1          0      0.9       0.0008
    '01/02/2000'   'koi'    1          1      5.5       1.8

can anyone help me?

Thanks in advance!

1
cell is a reserved function in MATLAB, do not obfuscate it by creating a variable with the same name. Also, I cannot reproduce the problem with the actual snippet of code. Consider changing cell to Cell and providing code that reproduces the issue. - Oleg
Oleg, my bad about the name. Its actually another name! - Luiz
i'm gratefull to your answer. I know that u answered something very similar to it at mathworks.com/matlabcentral/newsreader/view_thread/290278 But this second part did not worked for me! Could you help me? - Luiz
Please, modify your example such that we can run into your problem. You cannot pretend I can solve your issue by an arguable analogy with a question I answered 2years and 7 months ago! - Oleg
Oleg, I'm so sorry. I changed my example, is it clearer? - Luiz

1 Answers

0
votes

The problem are the headers in your m cell array.

You have to leave the headers out:

c = m([m{2:end,3}] == 1,:)

If you select the whole third column, you will get:

>> m{:,3}
ans =
number
ans =
     1
ans =
     2
ans =
     1
ans =
     2
ans =
     5

Note that the first cell in that column is of type char while the others are double.

If you concatenate them:

>> [m{:,3}]
ans =
number     

Be careful here, because the numbers did NOT disappear but they were converted to char, in fact:

>> double(ans)
ans =
   110   117   109    98   101   114     1     2     1     2     5

What happens now, is that the comparison is carried against this enlarged array:

>> [m{:,3}] == 1
ans =
     0     0     0     0     0     0     1     0     1     0     0

This is probably not the index you want.

To test your code, you can select a part of it and press F9. Only the highlighted code will be executed. Therefore, you don't need to type those commands in the cmd window to see what a portion of code does.