Search code examples
matlabstrcmpxlsread

Read specific portions of an excel file based on string values in MATLAB


I have an excel file and I need to read it based on string values in the 4th column. I have written the following but it does not work properly:

[num,txt,raw] = xlsread('Coordinates','Centerville');

zn={};
ctr=0;
for i = 3:size(raw,1)
    tf = strcmp(char(raw{i,4}),char(raw{i-1,4}));
    if tf == 0
        ctr = ctr+1;
    end
    zn{ctr}=raw{i,4};
end

data=zeros(1,10); % 10 corresponds to the number of columns I want to read (herein, columns 'J' to 'S')
ctr=0;
for j = 1:length(zn)
    for i=3:size(raw,1)
        tf=strcmp(char(raw{i,4}),char(zn{j}));
        if tf==1
            ctr=ctr+1;
            data(ctr,:,j)=num(i-2,10:19);
        end
    end
end

It gives me a "15129x10x22 double" thing and when I try to open it I get the message "Cannot display summaries of variables with more than 524288 elements". It might be obvious but what I am trying to get as the output is 'N = length(zn)' number of matrices which represent the data for different strings in the 4th column (so I probably need a struct; I just don't know how to make it work). Any ideas on how I could fix this? Thanks!


Solution

  • Did not test it, but this should help you get going:

    EDIT: corrected wrong indexing into raw vector. Also, depending on the format you might want to restrict also the rows of the raw matrix. From your question, I assume something like selector = raw(3:end,4); and data = raw(3:end,10:19); should be correct.

    [~,~,raw] = xlsread('Coordinates','Centerville');
    
    selector = raw(:,4);
    data     = raw(:,10:19);
    
    [selector,~,grpidx] = unique(selector);
    
    nGrp = numel(selector);
    
    out  = cell(nGrp,1);
    for i=1:nGrp
        idx      = grpidx==i;
        out{i}   = cell2mat(data(idx,:));
    end
    

    out is the output variable. The key here is the variable grpidx that is an output of the unique function and allows you to trace back the unique values to their position in the original vector. Note that unique as I used it may change the order of the string values. If that is an issue for you, use the setOrderparameter of the unique function and set it to 'stable'