Ignoring Simulink blocks when generating code
23 views (last 30 days)
Show older comments
I am trying to make stand alone blocks for a toolbox and have bumped into an issue getting them to work with the Simulink Coder.
Essentially, blocks have to output generated data to a file to be used at a later date. I got this to work by using a MATLAB function call and then using a combination of fopen and fprintf.
function fcn(u,FVar_M)
%#codegen
FileName = char(FVar_M);
FID = fopen(FileName,'w');
L = length(u);
for i = 1:L
u(i) = min(99999,u(i));
fprintf(FID,'%f\n',u(i));
end
fclose(FID);
This works to generate code unless I call my block from a model block (which I want to do). In this case I get some errors:
'coder.internal.fileManagerHelper' requires the use of EMLRT and can only be used with MEX and S-Function targets.
Function 'fileManager.m' (#34.1824.1835), line 45, column 52: "varargin{3}" Component: MATLAB Function | Category: Coder error
and
Function call failed.
Function 'fileManager.m' (#35.3243.3292), line 98, column 21: "coder.internal.fileManager('open',filename,cp,af)"
The functionality won't really be used when building code, so I got the bright idea of trying to disable the block when it wouldn't be required. I put the below code into the initialization for the mask, however i get an error saying you can't comment out blocks in a call back function.
if Set_M == 0
set_param(strcat(gcb,'/myblock','Commented','on');
else
set_param(strcat(gcb,'/myblock'),'Commented','off');
end
Any ideas on how I can:
A) fix my issue with the code gen when my block is within a model block? and/or
B) how to disable certain portions of a block when using code gen (note, i would like to know this second part regardless if the first part is figured out. I want to add some functionality into the blocks that is not appropriate for code gen).
Thanks, -Jeff
0 Comments
Answers (1)
Ryan Livingston
on 10 Jul 2015
Edited: Ryan Livingston
on 10 Jul 2015
This is a bug in MATLAB Coder when using file I/O with model reference (i.e. a model block) and either accelerator or rapid accelerator mode. It has been reported to our development teams.
From my experiments, simulating in normal mode should not be an issue. Does that work for you?
One way to disable parts of a MATLAB Function block is to use coder.target to conditionally include certain code:
if coder.target('Sfun')
simulationCode();
end
or:
if coder.target('Rtw')
standaloneAndRapidAccelCode();
end
For disabling Simulink blocks in general you could consider Conditional subsystems. These allow enabling and disabling parts of a model dynamically.
1 Comment
See Also
Categories
Find more on Simulink Coder in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!