Illegal use of reserved keyword "if"

13 views (last 30 days)

I'm trying to run this code below and I keep getting the message

"illegal use of reserved keyword if" in line 27. 

I'm also sending the .mat file if anyone fancies running it to see what happens. any thoughts?

Reference.Adipose = 950;
Reference.Bladder = 1000;
Reference.SkeletalMuscle = 1050;
Reference.Artery = 1060;
for i = 1:8 
    Name = data.Patient(i,1);
    Scanner = data.Scanner (i,1);
    Type = data.Type (i,1);
      x_optartery = [Reference.Adipose Reference.Adipose Reference.SkeletalMuscle...
         Reference.SkeletalMuscle Reference.Artery Reference.Artery];
      y_optartery = [data.Adipose(i,1) data.Adipose(i,2) data.Muscle(i,1)...
         data.Muscle(i,2) data.Artery(i,1) data.Artery(i,2)]
      x_all = [Reference.Adipose Reference.Adipose Reference.Bladder...
          Reference.SkeletalMuscle Reference.SkeletalMuscle...
          Reference.Artery Reference.Artery];
      y_all = [data.Adipose(i,1) data.Adipose(i,2) data.Bladder(i,1)...
          data.Muscle(i,1) data.Muscle(i,2) data.Artery(i,1) data.Artery(i,2);
     if data.Bladder (i) ~= 0 && data.Artery(i,2) ~= 0
          x = x_all;
          y = y_all - 1024;
     else data.Bladder(i) == 0
          x = x_optartery;
          y = y_optartery - 1024;
     end
      [curve] = fit(x,y,'poly1');
      figure
      hold on
      plot(x,y,'*')
      y2 = curve.p1*x + curve.p2; % this is the regression we found with our curve fit
      plot(x,y2)
      xlabel('HA Density [mg/cm^3]');
      ylabel('CT Number [HU]');
      str1 = ['Scanner: ',char(Scanner),', Patient: ',char(Name),];
      str2 = ['Type: ',char(Type)];
      title({str1;str2});
      str = ['y = ',num2str(curve.p1),'*HU + ',num2str(curve.p2)];
      text(1000, 960, str) % tells matlab where to put the equation
end

Accepted Answer

Image Analyst
Image Analyst on 21 Feb 2018
You can't do this:
else data.Bladder(i) == 0
You can have this:
elseif data.Bladder(i) == 0
or this:
else
data.Bladder(i) = 0
but not plain "else" followed by a condition. Use whichever you want to do (since I'm not sure what you're intending), though I imagine it's probably
elseif data.Bladder(i) == 0
  2 Comments
Arslan Abbas
Arslan Abbas on 23 Jul 2020
A bit late reply. but the problem is actually "unmatched square bracket" in the statement preceeding the if statement.
y_all = [data.Adipose(i,1) data.Adipose(i,2) data.Bladder(i,1)...
data.Muscle(i,1) data.Muscle(i,2) data.Artery(i,1) data.Artery(i,2);
There must be a "]" at the end of this statement.
Walter Roberson
Walter Roberson on 23 Jul 2020
Arslan is correct.
It is legal to have
else data.Bladder(i) == 0
and that means the same as
else
data.Bladder(i) == 0
which means the same as
else
disp(data.Bladder(i) == 0)

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!