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

2 次查看(过去 30 天)
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 Sarkar
Swastik Sarkar 2024-8-21
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 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