How can I used 'save()' command in matlab function block?

Hi, everyone. I really need everyone help. I can't used 'save()' command in matlab function block. Do anyone know? and how can I change the 'save()' command in the matlab function block?
function [DayOfOvule,Dmax,CycleLength] = detection(dataB,dataA,limit,CL)
%#codegen
A = dataA;
B = dataB;
fileD = 'difference.txt';
if (A > limit) && (B < limit)
D = A - B;
save(fileD, 'D');
elseif (A > limit) && (B > limit)
%startCountDown
else
end
CycleLength = CL;
S = load(fileD, 'D');
Dmax = max(S.D);
DayOfOvule = 13;
end
end
Does matlab can hold a value? For example, I have my data in timeseries and I want to calculate the differences between each data. Then, from differences I want to find the largest value of the different.
#credit to Walter Roberson for teach me something about delay.
%

5 个评论

You must specify the error you are getting.
Hi KSSV..
here are the error when I insert save(), load() command in the matlab function block.
Function 'save' is not supported for code generation and the coder.extrinsic directive cannot be applied to 'save'. Rewrite your code so it does not use 'save'.
Function 'MATLAB Function Differentiation' (#137.209.223), line 9, column 5: "save(fileD, D)" Launch diagnostic report. Component: MATLAB Function | Category: Coder errorOpen Failed to load file 'difference.txt': Number of columns on line 2 of ASCII file C:\Users\Lenovo\Documents\MATLAB\difference.txt must be the same as previous lines.
Function 'MATLAB Function Differentiation' (#137.329.345), line 17, column 9: "load(fileD, 'D')" Launch diagnostic report. Component: MATLAB Function | Category: Coder error Undefined function or variable 'S'. The first assignment to a local variable determines its class.
Function 'MATLAB Function Differentiation' (#137.362.363), line 18, column 16: "S" Launch diagnostic report.
I'm just curious...
What is the protocol code to use commands in the MATLAB function block? because until this minute I try to use different ways of 'saving or hold' the data like saving or holding the data in EEPROM.
For example:
  • if I'm used 'table' command
The 'table' class does not support code generation.
Function 'MATLAB Function Differentiation' (#38.235.243), line 11, column 9:
"table(D)"
Launch diagnostic report.
  • If I'm used 'writetable' command
The function 'writetable' is not supported for standalone code generation. See the documentation for coder.extrinsic to learn how you can use this function in simulation.
Function 'MATLAB Function Differentiation' (#38.250.312), line 12, column 5:
"writetable(D,'myData.csv','Delimiter',',','QuoteStrings',true)"
Launch diagnostic report.
  • If I'm used 'csvread' command
The function 'csvread' is not supported for standalone code generation. See the documentation for coder.extrinsic to learn how you can use this function in simulation.
Function 'MATLAB Function Differentiation' (#38.475.498), line 18, column 5:
"csvread('myData.csv',D)"
Launch diagnostic report.
  • If I'm used 'save' command
Function 'save' is not supported for code generation and the coder.extrinsic directive cannot be applied to 'save'. Rewrite your code so it does not use 'save'.
Does anyone know why?
Note that,
I'm using MATLAB function block to code a function in simulink.
You'll need to use a file I/O function that is supported for code generation. Two examples of that would be fwrite, for binary files, and fprintf, for text files.
These functions, while they work, are still not as convenient as using save due to the overhead associated with the particulars of using lower-level file I/O functions.
You might consider a different approach to saving data in your Simulink model that originates inside a MATLAB Function block. You can try outputting the data back onto a Simulink output port and then using the To File block to save the data to a .mat file. That might be cleaner in the end.
And yes, I realize this is almost 4 years after you submitted your question but I hope it helps someone out there.
Kerry

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Data Import from MATLAB 的更多信息

标签

提问:

2016-12-9

评论:

2020-11-13

Community Treasure Hunt

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

Start Hunting!

Translated by