Search code examples
excelmatlabcastingcell

MATLAB cell to string


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


Solution

  • 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);