Error: new fields cannot be added when structure has been read or used only in Simulink using Matlab Function
Show older comments
Hi everyone,
I have a question why the code
e=7
b(e);
function a = b(t)
cest.d=t;
cest.e=cest.d;
a=cest.e;
end
works fine in Matlab, but not in Simulink. In Simulink I create a Constant Block with the value 7, in addition I create a Matlab Function Block in which I insert this code

function a = b(t)
cest.d=t;
cest.e=cest.d;
a=cest.e;
end
and pass the obtained value to the Matlab workspace.
In theory, this should also work in Simulink. But I always get the error message This structure does not have a field 'e'; new fields cannot be added when structure has been read or used.
Function 'MATLAB Function' (#36.28.32), line 3, column 1:
"cest"
Launch diagnostic report.
What is the reason for this and how can I work around this error in the best case?
(The problem: I am working on a project where exactly this error occurs when trying to include a Matlab script that calls several other scripts where this type of assignment occurs and Simulink gives the error even in scripts that are not directly included in Simulink).
Thanks!
Answers (1)
Walter Roberson
on 21 Aug 2021
e=7
b(e);
function a = b(t)
cest = struct('d', 0, 'e', 0);
cest.d=t;
cest.e=cest.d;
a=cest.e;
end
3 Comments
Deniz
on 23 Aug 2021
Walter Roberson
on 23 Aug 2021
Interpreted MATLAB Block
Note that this can only be used when acceleration is turned off, or is at the lowest possible acceleration. The higher acceleration steps are intended to generate to an environment in which the MATLAB runtime is no longer available.
If you need acceleration together with MATLAB functions, then you will need to sanitize the MATLAB functions.
家锋
on 4 May 2023
thank you so much
Categories
Find more on Naming Conventions 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!