Why I get "Fcn Block has a syntax error"?

 Accepted Answer

I do not see anything obvious. You could try retyping it again without any spaces just in case there is an extra blank that I am overlooking. I suspect that will not help.
However, you appear to be using an Interpreted MATLAB Block. Interpreted MATLAB Blocks can only have a single input, and so only a single variable name. There does not appear to be any way for the code to know where to retrieve a0, af, bf, n, or p. If you want to use multiple inputs, even if the inputs are constants, then you need to use a MATLAB Function Block (formerly known as an Embedded Function Block).
There is a possibility that you might be able to code something like
evalin('base', 'a0') + evalin('base', 'af') .* cos(evalin('base', n') * ..... etc
I do not know if it would be able to recognize that code or not. If it did, then the constants would have to be in the base workspace similar to if you had been using a From Workspace block.
My guess is that Interpreted MATLAB Blocks can only refer to one variable in their code.

5 Comments

Thanks for your answer.
I retyped it without any spaces and I got error again.
About Matlab Function Block:
When I am writing this inside Matlab Function Block:
function y = fcn(u)
y = evalin('base', 'a0') + sum( evalin('base', 'af') .* cos(evalin('base', 'n') * evalin('base', 'zp') * u) + evalin('base', 'bf') .* sin(evalin('base', 'n') * evalin('base', 'zp') * u) );
end
I got this error "Function 'evalin' is not supported for code generation. Consider adding coder.extrinsic('evalin') at the top of the function to bypass code generation."
When I am writing this inside Matlab Function Block:
function y = fcn(u)
y = a0 + sum( af .* cos(n * zp * u) + bf .* sin(n * zp * u) );
end
I got this error "Undefined function or variable 'a0'"
When I am writing this inside Matlab Function Block:
function y = fcn(u)
y = 1 + sum ( [1 2 3 4 5 6 7 8] .* cos([1 2 3 4 5 6 7 8] .* 20 * u) + [1 2 3 4 5 6 7 8] .* sin([1 2 3 4 5 6 7 8] .* 20 * u) );
end
I don't get error.
%
Should I just let it like this? I mean I get the same result right? I just only have to change the values manual.
function y = fcn(u)
y = 1 + sum ( [1 2 3 4 5 6 7 8] .* cos([1 2 3 4 5 6 7 8] .* 20 * u) + [1 2 3 4 5 6 7 8] .* sin([1 2 3 4 5 6 7 8] .* 20 * u) );
end
The syntax error you were getting when you used an Interpreted Function block, not an MATLAB Function block. With an Interpreted Function block, you can only have a single variable, but there is a chance that you might be able to use evalin('base') to bring in variables. However, if you are trying to generate code for a target then you would not be able to use Interpret Function blocks.
MATLAB Function Block (previously known as Embedded Function block) is permitted to have multiple variables, including having multiple input ports. You would configure the appropriate values for a0 and so on to flow in as "signals" to the MATLAB Function block, and you would not use evalin('base') for that purpose. MATLAB Function Block are what you would use if you need to be able to generate code (or if you need to use Rapid Accelerator Mode)
There are three different ways to use MATLAB code inside of Simulink: (1) Interpreted MATLAB blocks; (2) MATLAB Function Blocks; and (3) S-Functions . For the purpose of a single expression such as you have, you would be wanting to use an Interpreted MATLAB block for simplicity but it is restricted in what it can do and is restricted to a single variable name (or so I deduce.) I suspect MATLAB Function block is what you want to use.
In conclusion, can I just write the function inside the Matlab Function Block like this?
function y = fcn(u)
y = 1 + sum ( [1 2 3 4 5 6 7 8] .* cos([1 2 3 4 5 6 7 8] .* 20 * u) + [1 2 3 4 5 6 7 8] .* sin([1 2 3 4 5 6 7 8] .* 20 * u) );
end
Instead of this:
function y = fcn(u)
y = a0 + sum( af .* cos(n * zp * u) + bf .* sin(n * zp * u) );
end
Because I am getting error If I don't.
I will still get the same result, won't I?
The only problem is that If I want to change the numbers I will have to open the Simulink and go to Matlab Function Block to change them, right?
One last thing, why I am getting this file "slprj" when I run my simulink model? I noticed that I get this file when I am using the Matlab Function Block?
To bring in values for a0, af and so on for a MATLAB Function Block, add input ports to the block and bring in the values from somewhere appropriate. Perhaps from a Constant Block.

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink in Help Center and File Exchange

Asked:

Bob
on 7 Mar 2016

Edited:

Bob
on 18 Mar 2016

Community Treasure Hunt

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

Start Hunting!