Clear Filters
Clear Filters

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

14 views (last 30 days)
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

Answers (1)

Swastik
Swastik on 21 Aug 2024 at 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.

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!