MATLAB Coder - How to use "save" command

조회 수: 28 (최근 30일)
Ved
Ved 2015년 3월 18일
답변: Bonyadi Arezoo 2022년 7월 14일
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 )
%#codegen
save('a','a');
end
then I am having trouble generating the appropriate .cpp/.h files using Codegen. Here's a screens shot
.
Any help will be appreciated.

답변 (2개)

Mike Hosea
Mike Hosea 2015년 3월 19일
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 )
%#codegen
coder.extrinsic('assignin');
% Input changes, and we want to remember this step.
a = a/2;
assignin('base','a1',a);
% Array changes again. Remember this value.
a = a + 1;
assignin('base','a2',a);
% 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.
  댓글 수: 2
Mike Hosea
Mike Hosea 2015년 3월 20일
Ah, I just realized how to do it in MATLAB (not for standalone code generation). You need a function
function mysave(varname,value)
assignin('base',varname,value)
save(varname,varname);
This is a .m file that you will need on the MATLAB path. You won't generate code for it. Then you call this function extrinsically in your own code. You can use coder.extrinsic for it, as above, or just
feval('mysave','a',a);
when you would have written
save('a','a');
Dipanshu Verma
Dipanshu Verma 2021년 1월 2일
@Mike I have tried creating my own function which is path detectable by MATLAB as it directly runs in console window too but still the error persists in my case.

댓글을 달려면 로그인하십시오.


Bonyadi Arezoo
Bonyadi Arezoo 2022년 7월 14일
Dear Mike, I wonder if you could answer this question:
https://uk.mathworks.com/matlabcentral/answers/1759570-real-time-matlab-speedgoat-simulink-read-write-data-solution

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by