Illegal use of reserved keyword "end", Error. Can some help me with it?

11 views (last 30 days)
Function [trans] = Trans_tensor2crystalori(o)
p1 = o.phi1;
p = o.Phi;
p2 = o.phi2;
trans(1,1) = cos(p1)*cos(p2) - sin(p1)*sin(p2)*cos(p);
trans(1,2) = cos(p2)*sin(p1) + sin(p2)*cos(p1)*cos(p);
trans(1,3) = sin(p2)*sin(p);
trans(2,1) = -cos(p1)*sin(p2) - sin(p1)*cos(p2)*cos(p);
trans(2,2) = -sin(p2)*sin(p1) + cos(p2)*cos(p1)*cos(p);
trans(2,3) = cos(p2)*sin(p);
trans(3,1) = sin(p1)*sin(p);
trans(3,2) = -cos(p1)*sin(p);
trans(3,3) = cos(p);
end;

Answers (2)

Star Strider
Star Strider on 11 Nov 2022
MATLAB is case-sensitive.
This:
Function [trans] = Trans_tensor2crystalori(o)
needs to be:
function [trans] = Trans_tensor2crystalori(o)
Try this instead —
function [trans] = Trans_tensor2crystalori(o)
p1 = o.phi1;
p = o.Phi;
p2 = o.phi2;
trans(1,1) = cos(p1)*cos(p2) - sin(p1)*sin(p2)*cos(p);
trans(1,2) = cos(p2)*sin(p1) + sin(p2)*cos(p1)*cos(p);
trans(1,3) = sin(p2)*sin(p);
trans(2,1) = -cos(p1)*sin(p2) - sin(p1)*cos(p2)*cos(p);
trans(2,2) = -sin(p2)*sin(p1) + cos(p2)*cos(p1)*cos(p);
trans(2,3) = cos(p2)*sin(p);
trans(3,1) = sin(p1)*sin(p);
trans(3,2) = -cos(p1)*sin(p);
trans(3,3) = cos(p);
end;
.
  2 Comments
Avez
Avez on 11 Nov 2022
Error: File: Taylor_TensorOri_transfunc.m Line: 14 Column: 4
This statement is not inside any function.
(It follows the END that terminates the definition of the function "Trans_tensor2crystalori".)
Got this error now
Star Strider
Star Strider on 11 Nov 2022
I have no idea what the problem is.
I only have access to what you posted (all of which now appears to be correct), and I can’t test the function because I have no idea what ‘o’ is.

Sign in to comment.


Walter Roberson
Walter Roberson on 11 Nov 2022
Moved: Image Analyst on 11 Nov 2022
The end on line 14 matches the function statement.
The semi-colon after the end on line 14 is then outside of any function.
MATLAB if ... end and while ... end and function ... end and switch ... end are not syntactically followed by semi-colon.
if rand() > 0.3
fprintf('Yes we have ');
end
fprintf('no bananas\n');
notice that there is no semi-colon after the end statement.
Semi-colon can act as a statement separator on the same line, such as
if rand() > 0.3; fprintf('Yes we have '); end
if debug; if rand() > 0.3; fprintf('Yes we have '); end; end
The semi-colon there is not part of the syntax of the inner if statement: it is general format for separating statements.
In most cases, extra semi-colons after end do not hurt: they just end up separating the previous statement from the "empty" statement after them.
if rand() > 0.3; fprintf('Yes we have '); end; %often valid
but that is only true while you are inside a function or script: once you have an end marking the end of a function, the termination of the function is immediately after the last character of the end that matches the function statement, and a semi-colon there would be code "after" the function.
  1 Comment
Image Analyst
Image Analyst on 11 Nov 2022
Yes, @Avez since your m-file is called "Taylor_TensorOri_transfunc.m" rather than "Trans_tensor2crystalori" it means your function is declared within an m-file that contains a script followed by a function called "Trans_tensor2crystalori". That function is "finished" by the end statement, as it must when you're combining functions with scripts in the same m-file. You must have the script first, then the function. But the semicolon after the end makes it think there is more script lines (indeed the semicolon is another script line of code). So either get rid of the semicolon like Walter says, or put Trans_tensor2crystalori into Trans_tensor2crystalori.m instead of putting it in your combined script/function file called "Taylor_TensorOri_transfunc.m".

Sign in to comment.

Categories

Find more on Creating and Concatenating 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!