Using fprintf in MATLAB function block with rapid accelerator (R2013b)

5 views (last 30 days)
I'd like to write output to a file from a MATLAB function block under certain conditions. The attached model illustrates simplistically what I'd like to do. Before running the model, I define a structure in the base workspace that will contain (among other things) the file pointer:
p.FID = [];
The MATLAB function block executes at 100 Hz, passes through the input to the output, and if the input is above a certain value, writes it to the file:
function y = fcn(u,p)
%#codegen
coder.extrinsic('fprintf')
y = u;
if u > 0.999
fprintf(p.FID,'Input value is above 0.999: %10.5f\n',u)
end
The MATLAB function block has an InitFcn callback to open the file and a StopFcn callback to close the file:
InitFcn:
p.FID = fopen('FOO.txt','w');
StopFcn:
fclose(p.FID);
p.FID = [];
Running in Normal or Accelerator mode produces the expected output in FOO.txt:
Input value is above 0.999: 0.99917
Input value is above 0.999: 0.99953
Input value is above 0.999: 0.99978
Input value is above 0.999: 0.99994
Input value is above 0.999: 1.00000
Input value is above 0.999: 0.99996
Input value is above 0.999: 0.99982
Input value is above 0.999: 0.99957
Input value is above 0.999: 0.99923
Input value is above 0.999: 0.99903
Input value is above 0.999: 0.99942
Input value is above 0.999: 0.99971
Input value is above 0.999: 0.99990
Input value is above 0.999: 0.99999
Input value is above 0.999: 0.99998
Input value is above 0.999: 0.99987
Input value is above 0.999: 0.99966
Input value is above 0.999: 0.99935
But running in Rapid Accelerator mode produces an empty file. An error occurs (invalid file identifier) if I use R2012b or earlier.
Can I write to a file using this approach in Rapid Accelerator mode with some minor mods or do I need to take a totally different approach?
Thanks,
--Chuck

Accepted Answer

Ryan Livingston
Ryan Livingston on 18 Mar 2014
Edited: Ryan Livingston on 18 Mar 2014
FOPEN, FCLOSE and FPRINTF were supported for code generation in R2013a:
You could omit the
coder.extrinsic('fprintf')
and simply make the calls to those functions in your MATLAB Function Block as you would in MATLAB and the necessary code will be generated for you that uses the corresponding C runtime routines.
When doing a Rapid Accelerator build, extrinsic calls are removed. Think of the Rapid Accelerator mode as first doing standalone code generation then wrapping the generated code so it can be called in Simulink:
As a result, the extrinsic calls to FPRINTF are not being used in rapid accelerator.
  1 Comment
Chuck Walker
Chuck Walker on 18 Mar 2014
Edited: Chuck Walker on 18 Mar 2014
I tried just eliminating the coder.extrinsic statement, but got the same behavior. Based upon your suggestion, though, I removed the InitFcn callback and opened the file in the MATLAB Function Block itself, keeping the file pointer as a persistent variable:
function y = fcn(u,p)
%#codegen
persistent FID
if isempty(FID)
FID = fopen('FOO.txt','w');
end
y = u;
if u > 0.999
fprintf(FID,'Input value is above 0.999: %10.5f\n',u);
end
Then i did an 'fclose all' in the block's StopFcn. This works in all modes.
Thanks for your help, Ryan!

Sign in to comment.

More Answers (0)

Categories

Find more on Event Functions 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!