Writing to file in compiled c code from simulink model, on Raspberry Pi
2 views (last 30 days)
Show older comments
Jonas Landberg
on 27 Mar 2015
Commented: Ryan Livingston
on 31 Dec 2015
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
Jonas Landberg
on 2 Apr 2015
1 Comment
Adil Faiz
on 28 Dec 2015
Hey, i need some help ,I m building a robot using vision for line following,is it possible that i can code in matlab for pi and made it as autonomous by image processing ,i mean do i have to give live commands to pi through wired or wireless connection to pi for processing,may be i am not clear as i am naive using raspberry pi for image processing .
More Answers (0)
See Also
Categories
Find more on Raspberry Pi Hardware 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!