Writing to file in compiled c code from simulink model, on Raspberry Pi
Show older comments
Hello,
I am trying to write to file in a simulink model that I will later deploy and run as standalone on the Pi.
I have tried a few different versions with no success. When run in external mode in simulink it works with all the solutions, but when i run the built executable from the modelname/MW/ folder on the pie, no file is written.
function savePoints(points, time)
%#codegen
coder.cinclude('<stdio.h>');
format = ['%d ', 0];
formatTime = ['%f ', 0];
fname = coder.nullcopy(uint8(zeros(1, 32)));
coder.ceval('sprintf', coder.wref(fname), ['testPoints.dat', 0]);
fd = coder.opaque('FILE *');
fd = coder.ceval('fopen', fname, ['a', 0]);
coder.ceval('fprintf', fd, formatTime, time);
for i = 1:272
coder.ceval('fprintf', fd, format, int32(points(i)));
end
coder.ceval('fprintf', fd, [char(10) 0]);
coder.ceval('fclose', fd);
Above you can see one attempt, which is a rewamp of the motion sensor example code provided by Mathworks.
I have studied the c code, and it seems "right" as far as I can see, it should write a new line to the file each cycle in the model, and close the file each time. As such, even if it is terminated early/not at all, at some point there should be a file created?
Anyone have any experience with these type of issues? As I said, it works fine when run in external mode.
Have also tried with the "tofile" block, but I have a feeling I have seen somewhere that it does not compile. It behavs similar to my code anyway. Any help would be appriciated!
1 Comment
Ryan Livingston
on 31 Dec 2015
MATLAB Coder supports generating code for the MATLAB fopen, fprintf, and fclose starting in R2013a. You could consider using those and skipping the coder.ceval code if using a sufficiently new version.
Accepted Answer
More Answers (0)
Categories
Find more on Custom Device Driver Blocks 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!