1

I have a cell array that is a list of file names. I transposed them because I find that easier to work with. Now I am attempting to go through each line in each cell and remove the lines based on their file extension. Eventually, I want to use this list as file names to import data from. This is how I transpose the list

 for i = 1:numel(F);
    a = F(1,i);
    b{i} = [a{:}'];
 end;

The code I am using to try and read the data in each cell keeps giving me the error input must be of type double or string. Any ideas?

for i = 1:numel(b);
    for k = 1:numel(b{1,i});
        b(cellfun(textscan(b{1,i}(k,1),'%s.lbl',numel(b)),b))=[];
    end;
end;

Thanks in advance.

EDIT: This is for MATLAB. Should have been clear on that. Thanks Brian. EDIT2: whos for F is

Name      Size               Bytes  Class    Attributes
b         1x11            13986188  cell 

while for a is

 Name      Size             Bytes  Class    Attributes
 a         1x1             118408  cell
  • OK, I added the matlab tag to your question. That will help get the right people to look at it. – Brian Rogers Mar 24 '13 at 21:16
  • Could you add to your post the result of typing whos F at the command prompt as well as whos a after running your code? – grantnz Mar 24 '13 at 21:23

2 Answers2

1

From your description I am not certain how your F array looks, but assuming

F = {'file1.ext1', 'file2.ext2', 'file3.ext2', 'file2.ext1'};

you could remove all files ending with .ext2 like this:

F = F(cellfun('isempty', regexpi(F, '\.ext2$')));

regexpi, which operates on each element in the cell array, returns [] for all files not matching the expression. The cellfun call converts the cell array to a logical array with false at positions corresponding to files ending with .ext2and true for all others. The resulting array may be used as a logical index to F that returns the files that should be kept.

erikced
  • 712
  • 4
  • 13
0

You're using cellfun wrong. It's signature is [A1,...,Am] = cellfun(func,C1,...,Cn). It takes a function as first argument, but you're passing it the result of textscan, which is a cell array of the matching strings. The second argument is a cell array as it should be, but it doesn't make sense to call it over and over in a loop. `cellfun´'s job is to write the loop for you when you want to do the same thing to every cell in a cell array.

Instead of parsing the filename yourself with textscan, I suggest you use fileparts

Since you're already looping over the cell array in transpose-step, it might make sense to do the filtering there. It might look something like this:

for i = 1:numel(F);
    a = F(1,i);
    [~,~,ext] = fileparts(a{:});
    if strcmpi(ext, '.lbl')
        b{i} = [a{:}'];
    end
end;
Kleist
  • 7,785
  • 1
  • 26
  • 30