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

2 Ansichten (letzte 30 Tage)
Alvaro Navarro
Alvaro Navarro am 22 Mär. 2019
Beantwortet: Swastik Sarkar am 21 Aug. 2024
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

Antworten (1)

Swastik Sarkar
Swastik Sarkar am 21 Aug. 2024
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.

Kategorien

Mehr zu MATLAB Coder finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by