Conditional / If Statements not running

I have a code as follows:
Thresh=250;
if(Difference(:,1)>Thresh)
disp('Seizure')
else
disp('Nil')
if(Difference(:,2)>Thresh)
disp('Seizure')
else
disp('Nil')
if(Difference(:,3)>Thresh)
disp('Seizure')
else
disp('Nil')
if(Difference(:,4)>Thresh)
disp('Seizure')
else
disp('Nil')
if(Difference(:,5)>Thresh)
disp('Seizure')
else
disp('Nil')
if(Difference(:,6)>Thresh)
disp('Seizure')
else
disp('Nil')
if(Difference(:,7)>Thresh)
disp('Seizure')
else
disp('Nil')
if(Difference(:,8)>Thresh)
disp('Seizure')
else
disp('Nil')
end
end
end
end
end
end
end
end
It seems to stop running when 'Seizure' is returned for the first time. E.g. if seizure is returned on the third if, it does not return me anything for the remaining 5 ifs. Any help or suggestions would be greatly appreciated!! :)

4 Comments

Adam
Adam on 26 Feb 2016
Edited: Adam on 26 Feb 2016
Please format your code in a {} Code block as it is very difficult to read in that format!
It would also be useful to see your data though - what is in Difference(:,1:8) and what size is Difference?
If Difference(:,1) is returning multiple values (i.e. you have a true 2d matrix) then your > comparison may also not be doing what you expect it to as a logical comparison.
Hi Laura,
it works properly. When the statement is satisfied (in your case after the third value of Difference, for ex. for Difference = [249:1:256]; ) then if statement is concluded. If you want to check all the values of the Difference array you can use a For cycle:
Difference = [248:1:255];
Thresh=250;
for i = 1:length(Difference)
if (Difference(:,i)>Thresh)
disp(['Element ',num2str(i),' Seizure'])
else
disp(['Element ',num2str(i),' Nil'])
end
end
Best regards
angelo
Hi Angelo Thank you for your response! I tried your suggestion and it certainly works to make the whole string of conditions run, but it returns the wrong answers. It returns Nil for everything even when it should be Seizure. Any suggestions?
Apologies Adam, I did not realise it would come out that way... I shall know for the future. Thank you if it was you who fixed it for me.
The first bit of code looks like this
SeizureData=Seizure1;
SeizureData=reshape(Seizure1, [],9);
AbsoluteSeizure=abs(SeizureData);
Amplitude=mean(AbsoluteSeizure);
Difference=diff(Amplitude);
I started with 1557 values all in one column (Seizure1).
An example of how the difference values come out are:
-25.6878612716763 410.936416184971 -75.5028901734104 -317.329479768786 391.763005780347 -399.895953757225 9.61849710982659 311.416184971098
I have 6 different scripts, one for each piece of data i've imported/created....

Sign in to comment.

 Accepted Answer

Guillaume
Guillaume on 26 Feb 2016
Edited: Guillaume on 26 Feb 2016
Maybe if you'd use indentation in your program, you'd have seen straightaway what it is doing. This is your code with proper indentation:
if cond1
disp('Seizure')
else
disp('Nil')
if cond2
disp('Seizure')
else
disp('Nil')
if cond3
disp('Seizure')
else
disp('Nil')
if cond4
...
end
end
end
end
As you can see, you move to the next test only if the condition is satisfied. What you wanted was:
if cond1
disp('Seizure')
else
disp('Nil')
end
if cond2
disp('Seizure')
else
disp('Nil')
end
...
It's lucky you only have 8 conditions though. What would you have done if you had 100? Copy/paste and edit the ifs a 100 times. Using a loop as per Angelo's comment is more efficient (and less error prone), but even more efficient:
words = {'Nil'; 'Seizure'};
Thresh=250;
isseizure = Difference > Thresh;
disp(char(words(all(isseizure, 1)+1)))
No loop or if needed

3 Comments

Hi Guillame, thank you for your response! I have tried it and it works! Would you mind explaining to me what your code means/does? Yes, I know it's lucky I've only 8 conditions! I couldn't work out how to do a loop so I just stuck with a small number rather than increasing my samples... maybe now I can :) Also, if you wouldn't mind explaining the indentation to me more? I've never used Matlab before this so I didn't know it made a difference.
Indentation makes no difference to the way the code runs, so you don't have to use it. It's purely a visual thing that helps you the programmer make better sense of the flow of the program.
If you look at your original code, it's difficult to see whether the ifs are nested or follow one another. In my indented version, you can more clearly see that each if is nested within the else of the previous if and so the test is only carried out if the previous test failed.
My comment about you being lucky for having only 8 conditions is to make you realise that repeating code that perform the same action with only a small variation is not a good idea and simply doesn't scale. Programmers should be lazy and let the computer do the repeating for them. The simplest solution (not always the most efficient) is to wrap the repeated action in a for loop as per Angelo's comment.
Now for the code in my answer: You want to compare the values of a vector to a single threshold. That can be done in one go, and is simply:
isseizure = Difference > Thresh;
isseizure is a logical array with the same number of elements as Difference and each value is either 1 (for true, when the element in Difference is greater than Thresh) or 0 (for false).
What you want to do is output 'Nil' when an element of isseizure| is 0 and 'Seizure' when it's 1, so the first thing I did is create a cell array of the two strings in that order:
words = {'Nil'; 'Seizure'};
Next, for for each 0 in isseizure you want to pick the 1st word and for each 1, you want to pick the 2nd one. So, simply add 1 to isseizure and use that as an index in words:
result = words(isseizure + 1); %slight difference to my original answer that will be explained later
result is a cell array, the same size as isseizure where each element is 'Nil' when isseizure is 0 and 'Seizure' when 1. You could actually keep it like that if you wanted to do further processing or just display the cell array directly, but in order to get the exact same output as you had, I convert the cell array or string into a char array and pass that to disp. For that last step to work properly, it's important that the elements of words are in a single column.
Note that I've slightly simplified the code from my original answer to:
disp(char(words(isseizure + 1)));
That's because I wasn't sure whether or not your Difference was a vector or a matrix (you used 2D indexing in your code). As your Difference is a vector, you don't need the all call.
Laura: Try this trick.
  1. Put your cursor anywhere in your text editor window with your code.
  2. Type control-a (to select all the text)
  3. Type control-i (to properly indent all the code)

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!