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

조회 수: 2 (최근 30일)
Alvaro Navarro
Alvaro Navarro 2019년 3월 22일
답변: Swastik Sarkar 2024년 8월 21일
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