フィルターのクリア

Converting a C char array into a Matlab String [Matlab Coder]

14 ビュー (過去 30 日間)
Alvaro Navarro
Alvaro Navarro 2019 年 3 月 22 日
回答済み: Swastik 2024 年 8 月 21 日 3:35
My intention is to show for the output of Matlab System the char_T data[ ]
I've got two questions:
  • How to declare the buffer (#1) variable to store a char_T data[ ]
  • (#2) How do I dump buffer data through the output
% FILE.m
function data = stepImpl(obj)
buffer = ¿¿ ?? (#1) ;
if coder.target ('Rtw')
coder.cinclude('File_Wrapper.h');
coder.ceval('function',obj.port, coder.wref(buffer));
data = ¿¿¿ string(buffer) ??? (#2); % data - output Matlab System
end
end
Any suggestion is welcome!
% File_Wrapper.c
void function(uint8_T port, char_T data[])
{
if (obj.port == 1){
char buffer[30];
fgets(buffer, 10, uart1);
snprintf (data, sizeof(buffer), "%s", buffer);
}
}
This post dont work for me:
Thanks

回答 (1 件)

Swastik
Swastik 2024 年 8 月 21 日 3:35
You can declare an empty buffer in MATLAB using the “char and “zeros” functions, and then convert it back to a string using the “string function. Here's how you can do it in your “FILE.m:”:
% FILE.m
function data = stepImpl(port)
buffer = char(zeros(1,30)); % Adjust size accordingly
if coder.target('Rtw')
coder.cinclude('File_Wrapper.h');
coder.ceval('function', port, coder.wref(buffer));
data = string(char(buffer));
end
end
I have used the following command to generate code.
codegen -config:lib stepImpl -args {0.0} -launchreport
I hope this helps.

カテゴリ

Help Center および File ExchangeMATLAB Coder についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by