MATLAB: Writing to file in compiled c code from simulink model, on Raspberry Pi

code generationmatlab coderraspberry pisimulinkstandalone application

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!

Best Answer

Got help from the Mathworks support. If anyone have similar problems in the future:
It does work, but if you want to run the standalone version on the pi, build the code using normal and not external. So use realtime target, select the pi as a target, running mode normal, then ctrl-b in the toplevel of the model. Good luck!