MATLAB Coder - How to use "save" command
14 views (last 30 days)
I am using MATLAB (R2013a) Coder to generate the appropriate C++ files. I am unable to use the 'save' command when I am trying to save my processed matrices. Cutting the chase, if I have a simple function like the following (that just saves the variable as .mat file, and also returns the same),
function a = savefun( a )
then I am having trouble generating the appropriate .cpp/.h files using Codegen. Here's a screens shot
Any help will be appreciated.
Mike Hosea on 19 Mar 2015
Unfortunately, the SAVE command is not supported for code generation, and it doesn't make sense to call it as an extrinsic function. Since you are running R2013a, you should be able to use FOPEN, FCLOSE, and FPRINTF to write a text file with your data in it. Note that FPRINTF for code generation will require you to write your array elements with a loop (or loops), since it doesn't support the automatic looping that MATLAB's FPRINTF does. It will be more cumbersome to use the text files back in the MATLAB environment, but it should work.
The only alternative I might suggest, if your need is limited to returning intermediate stages for, say, debugging purposes, is to use ASSIGNIN as an extrinsic function.
function a = dosomething( a )
% Input changes, and we want to remember this step.
a = a/2;
% Array changes again. Remember this value.
a = a + 1;
% Array changes again, but this is the return value.
a = cos(a);
Then your calling program can use the SAVE command to write mat files if you need them.