Is there a command that can check if a string is present in a certain array and output that same string?

Hi i want to make checks to some string array with variable dimension and i tried doing what's written in the code. I know the problem is the code in the "if" but i don't know what to do... All the functions i know that find a string in an array return an array of logical 0 and 1. I can transform the array in the same dimension by putting 0 in all the seasons that aren't present and add some || in the if statement but i feel it's less practical, and i simply want to know if it's possible.
%The first column is for other purposes
L = {1, ["Spring", "Summer"]; 0, ["Winter", "Summer","Autumn"]}
i=0;
while (i<2)
i = i+1;
if (L{i,2}(:)=="Winter")
fprintf("Yes")
end
end

 Accepted Answer

I'm confused about what you want the output to be.
L = {1, ["Spring", "Summer"]; 0, ["Winter", "Summer","Autumn"]};
ism = cellfun(@(x)ismember("Winter",x),L(:,2))
ism = 2×1 logical array
0 1

7 Comments

I initially wanted the output to be the same string that i was checking. I noticed you used the ismember function that I knew nothing about and i simply used it in the same while loop i wrote, so the output is now a logical 1 or 0 for every row i'm checking and, by checking them one at a time, every output is now a single number. That pretty much resolved my issue, so thanks a lot for your help. Another thing i don't know is the @(x) you wrote next to ismember, what exactly does it do?
The @(x) creates an anonymous function of x. This is necessary for using cellfun, which runs ismember("Winter",x) where x is each string array in L(:,2), in turn (i.e., L{1,2}, then L{2,2}, and so on).
Essentially, using cellfun replaces your loop.
Sorry i can't understand when the function ismember retrieve L(:,2) as an input for x. I just read how cellfun and anonymous function works and i only saw anonymous functions working with a clear input from somewhere. Does the program automatically retrieve the input when calling the anonymous function? Excuse me if i'm annoying or inappropriate, I literally started this project with 0 prior knowledge on MATLAB, so i'm trying to learn from anywhere. Thanks a lot for your responses
"Does the program automatically retrieve the input when calling the anonymous function?"
That's what cellfun does, so, yes.
Here's another example of cellfun:
C = {[1 2 3],[4 5 6 7],[8 9]};
% the function @(x)x(1) merely returns the first element of x,
% so when run via cellfun on C as above, the result should be the first
% element of each vector contained in C, i.e., [1 4 8]
cellfun(@(x)x(1),C)
ans = 1×3
1 4 8
Oh ok so you're using @(x) to create a function inside the "function" input of cellfun that has C as an input. I understand now, thanks you for all your explanations
Exactly. It's the same as this:
C = {[1 2 3],[4 5 6 7],[8 9]};
f = @(x)x(1);
cellfun(f,C) % apply the function f to the contents of each cell of C
ans = 1×3
1 4 8
ism = cellfun(@(x)ismember("Winter",x),L(:,2))
is the same as
InternalTemporaryVariable = @(x)ismember("Winter",x);
ism = cellfun(InternalTemporaryVariable, L(:,2));
clear InternalTemporaryVariable
That is, the temporary anonymous function is created before cellfun is called, and is removed after the call (because there are no more references to it.)

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!