Using fprintf in MATLAB function block with rapid accelerator (R2013b)
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu MATLAB Function Block Basics finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!