simulink tlc如何通过tlc写数据入文件
Target Language Compiler Directives
一个sfunction的tlc的基本结构如下:
%% Copyright 1990-2009 The MathWorks, Inc.%implements "tt1" "C"%function BlockInstanceSetup (block, system) void%endfunction%% Function: Outputs ===========================================================
%%
%function Outputs(block, system) Output%endfunction%% [EOF]
- 快速入门
设计sfunction时,如果需要代码的到其他c文件下面,而不是自己block所在的c文件,只要用openfile、closefile,举例:
%% Copyright 1990-2009 The MathWorks, Inc.%implements "tt1" "C"%function BlockInstanceSetup (block, system) void%endfunction%% Function: Outputs ===========================================================
%%
%function Outputs(block, system) Output%openfile srcFile = "init111.c""hello word"%closefile srcFile%endfunction%% [EOF]
生成代码时,会自动生成名为 init111.c的c文件,并且里面自动写入“hello word”。
2.如果需要在c文件中追加数据而不是覆盖,
语法为:%openfile "c文件名", "a",举例:
%% Copyright 1990-2009 The MathWorks, Inc.%implements "tt1" "C"%function BlockInstanceSetup (block, system) void%endfunction%% Function: Outputs ===========================================================
%%
%function Outputs(block, system) Output%openfile srcFile = "init111.c","a""hello word333"%closefile srcFile%endfunction%% [EOF]
如果这个模型只有1个sfunction,那么它生成代码时,每次都会覆盖原文件,而不是追加,里面的字符串就是只有“hello word333”.
如果这个模型只有2个sfunction及以上的sfunction,那么它生成代码时,就会在后面追加数据,有多少个sfunction,init111.c中就会有多少行"helll word333"。
如果单独把下面的tlc单独拎出来,例如保存为test.tlc,在MATLAB命令行调试,并执行:tlc test.tlc,就会直接在后面追加数据:
%openfile srcFile = "init111.c","a""hello word333"%closefile srcFile