2
votes

I am trying to read an excel sheet and then and find cells that are not empty and have date information in them by finding two '/' in a string but matlab keeps to erroring on handling cell type "Undefined operator '~=' for input arguments of type 'cell'." "Undefined function 'string' for input arguments of type 'cell'." "Undefined function 'char' for input arguments of type 'cell'."

MyFolderInfo = dir('C:\');  

filename = 'Export.xls';
[num,txt,raw] = xlsread(filename,'A1:G200'); 

for i = 1:length(txt)
    if ~isnan(raw(i,1))
        if sum(ismember(char(raw(i,1)),'/')) == 2 
            A(i,1) = raw(i,1);
        end
    end
end

please help fixing it

2

2 Answers

3
votes

There are multiple issues with your code. Since raw is a cell array, you can't run isnan on it, isnan is for numerical arrays. Since all you're interested in is cells with text in them, you don't need to use raw at all, any blank cells will not be present in txt.

My approach is to create a logical array, has_2_slashes, and then use it to extract the elements from raw that have two slashes in them.

Here is my code. I generalized it to read multiple columns since your original code only seemed to be written to handle one column.

filename = 'Export.xls';
[~, ~, raw] = xlsread(filename, 'A1:G200'); 

[num_rows, num_cols] = size(raw);
has_2_slashes = false(num_rows, num_cols);
for row = 1:num_rows
   for col = 1:num_cols
      has_2_slashes(row, col) = sum(ismember(raw{row, col}, '/')) == 2;
   end
end

A = raw(has_2_slashes);
1
votes
cellfun(@numel,strfind(txt,'/'))

should give you a numerical array where the (i,j)th element contains the number of slashes. For example,

>> cellfun(@numel,strfind({'a','b';'/','/abc/'},'/'))
ans =
     0     0
     1     2

The key here is to use strfind.

Now you may want to expand a bit in your question on what you intend to do next with txt -- in other words, specify desired output more, which is always a good thing to do. If you intend to read the dates, it may be better to just read it upfront, for example by using regexp or datetime as opposed to getting an array which can then map to where the dates are. As is, using ans>=2 next gives you the logical array that can let you extract the matched entries.