Using IF and OR statements on multiple conditions

Hi, I have a set of conditions I would like to join together to function as just one set of conditions.
for i = 1:length(mydata)
if (mydata(i) == 24)
if (mydata(i+1) == 12)
mydata (i+1) = 'Done';
end
end
end
This works fine, just like my second set of conditions
for i = 1:length(mydata)
if (mydata(i) == 32)
if (mydata(i+1) == 36)
mydata(i+1) = 'Done';
end
end
end
I have also tried to join my individual if statements using &&, and this does not work, so this was my workaround.
However my main problem is that I would like to join these two functions using an || statement, something just like
if (i == 24)
if (i+1 == 12)
i+1 = 'Done'; || if (i == 32)
if (i+1 == 36)
i+1 = 'Done';
end
end
end
end
For some reason, MATLAB does not recognize the symbols, and my output does not change.
Any help would be appreciated.
Context: I want to write the function such that when 12 comes after 24 OR when 36 comes after 32, it shows Done, as in the screenshots below
Screen 1.PNG
to
Screen 2.PNG

7 Comments

Ukeje - one problem might be with the code
i+1 = 'Done'
What are you trying to do with this assignment? i is an integer and yet you want to assign a string to it? Or are you wanting to update an array with this string at index i+1.
As for combining your conditions, you need to use the equality operator == not the assignment operator =. But how can you have i equal to 24 and i+1 equal to 36? Again, I think you mean these to be indices into an arrays. Please clarify.
Hi Geoff,
Thanks for the quick response. I just saw the mistake. I have updated the question to include ==.
I am trying to update the value of i+1 to show as done. Screen 1 is a screenshot of the data I am trying to update.
Screen 1.PNG
I am trying to get MATLAB to update the values in i+1 to display as
Screen 2.PNG
In other words, I want to write the function such that when 12 comes after 24 OR when 36 comes after 32, it shows Done.
Thank you for the response in advance.
For some reason, MATLAB does not recognize the symbols
It's not for some reason, it's because what you wrote makes no sense and is not valid syntax. if you look at the documentation of the short-circuit operators, nowhere are they used to link statement together.
if (mydata(i+1) == 12)
mydata (i+1) = 'Done';
This works fine
Unlikely! Unless mydata is some special type that is happy to store heterogeneous data. None of the built-in matlab types do that and if mydata is double, the above will cause an error.
Rather than giving us some code that do no work, why don't you explain clearly what you want to do. Note that it's very likely that you don't need loops or if statement
if mydata(i) == 24 && mydata(i+1) == 12 || mydata(i) == 32 && mydata(i+1) == 36
mydata(i+1) = 'Done'
This is what I want to have MATLAB do in a sense.
Assuming that mydata is a type that can store both numbers and text using the () operator, and that uses == for comparison with either, your line above would work (as long as you stop iterating one element before the last of course).
So what is the class of mydata? For that matter, what is the size as well. It looks like you want to do that to the first column only.
class(mydata)
size(mydata) %please give us the output of these two lines
The Class is 'struct'
and the size is '1 1800'
Oh! Well, then isit's very unclear how your data is stored. Best way to clarify it is for you to attach a mat file with mydata in it.
structures are not typically used to store tabular data as you show in your examples.

Sign in to comment.

Answers (0)

Categories

Find more on Scripts in Help Center and File Exchange

Products

Release

R2018a

Asked:

on 12 Mar 2019

Commented:

on 12 Mar 2019

Community Treasure Hunt

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

Start Hunting!