Search code examples
matlabfor-loopif-statementcontainsmatlab-deployment

Confusion about If statement output in my MATLAB code using contains()


Apologies if a similar question has already been asked.

I am trying to compare two lists of company names and have created a sequence of if statements within a for loop, with a contains() function in the if statements.

The logic is that for each company name X in the original list, the if statements will be computed:

  1. if : If there is a match in the top 50 of both lists, then I get the name of the company with the statement "X is in the 2020 top 50 list".
  2. elseif : If there is a match in the top 50 of the original list and the rest of the secondary list then I get the name of the company with the statement "X is in the 2020 overall list"
  3. else : If there is no match, I get the name of the company that did not match with the statement "X is not in the 2020 list"

As of now my output is all the companies that are matches but with statement in (3) i.e. my else statement. Not sure why, but my else statement is capturing my if and elseif statement output.

Code:

year1 = data(1:end, 1:4); %tables
year30 = data(1:end, 121:124);
names30_50 = year30{1:50, 2}; %cells
names30_all = year30{:, 2};
names1_50 = year1{1:50, 2};
names1_all = year1{:, 2};


for i = 1:height(names1_50)
    if contains(names30_50, names1_50{i})  %comparing both top 50 lists
        disp(names1_50{i})
        fprintf('%s is in the 2020 top 50 list.\n',names1_50{i})
    elseif contains(names30_all, names1_50{i})  %comparing top 50 of 1990 with all of 2020
        disp(names1_50{i})
        fprintf('%s is in the 2020 overall list.\n',names1_50{i})
    else
        fprintf('%s is not in the 2020 list.\n',names1_50{i});
    end
end

Output example:

General Motors is not in the 2020 list.
Ford Motor is not in the 2020 list.
Exxon Mobil is not in the 2020 list.
Intl. Business Machines is not in the 2020 list.
General Electric is not in the 2020 list.
Mobil is not in the 2020 list.
Altria Group is not in the 2020 list.
Chrysler is not in the 2020 list.

It goes on, but all of these companies are in both lists.


Solution

  • The contains() function is used to evaluate if a pattern is present in a string or char. Example you have a char ch='my_char';

    %You have a char
    ch = 'my_char';
    %and another piece of char, the pattern
    pattern = 'char';
    %Evaluate if pattern is in my_char
    disp(contains(ch, pattern))
    % It returns true
    

    (sorry I do not know how to include matlab code)

    Here your 1st argument is a cell if I get it right. In this case the contains function returns a logical array. For each element of your cell names30_50, you know if it contains the names1_50{i} char.

    But, if an if-statement is given a logical array, it will execute the bloc only if ALL element in the array are true. In your case it will obviously never happen.

    What you need to do is to write

    if any(contains(names30_50, names1_50{i}))
    ...
    

    The any() function returns true if at least one element in the logical array is true.

    By the way I would rather use the strcmp() function instead of contains().

    I hope I understood correctly your problem.