1
votes

I am trying to grab data from an excel spread sheet and grab only the information from cells that match a string. Eg. if cell A10 contains the word 'Canada' it should return that cell.

I have tried using strcmp(https://www.mathworks.com/help/matlab/ref/strcmp.html) to check if string in argument 1 is contained in a cell array containing many strings, the second argument

[num,txt,raw] = xlsread('\\Client\C$\Users\Fish\Desktop\dataset\dataset.csv');

mytable = cell(raw); 


for i = 1:54841 
    array_index = i;
    string_index = mytable(i,2);
    string_eastern = {'Canada', 'Ontario'};

    if strcmp(string_index,string_eastern);
       fprintf('%d\n',array_index)
    end
end

In the above example if my string_eastern only contains one element, say 'Canada', it will return the index value of every instance of 'Canada'. If I add more elements I expect it would return index values for every instance where string_index would match with a string contained in string_eastern. However I get no results at all if I add more elements.

Pretty much I wanted to check my string_index agaisnt string_eastern, if the values match then I want it to return that cell value. This works when string_eastern is only 1 element but does not work with more than 1.

1
You need not to run a loop and check every cell. You have functions for that to get what you want in a single instant. Read about the function contains.Siva Srinivas Kolukula

1 Answers

1
votes

To access cell contents, use the curly brackets {}. So if I wanted to access the first element of my cell, I would say this:

string = cell{1};

Read more on the MATLAB Documentation about cells to learn more and to answer any of your further questions.