使用Matlab Coder将C char数组转换为Matlab字符串 [英] Converting a C char array into a Matlab String using Matlab Coder

查看:306
本文介绍了使用Matlab Coder将C char数组转换为Matlab字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些用Matlab编写的代码,正在使用Matlab Coder转换为C代码.

I have some code written in Matlab that I am converting into C code using Matlab Coder.

我编写了一个C函数,该函数要从生成的C代码中调用.到目前为止效果很好.

I have written a C function that I want to call from the generated C code. This works, so far so good.

问题在于C函数的参数之一是用于输出字符串的char **.

The problem is that one of the parameters to the C function is a char** used to output a string.

我通过制作一个不透明的char *并使用coder.wref来获取char **来实现这一点.但是,如何将不透明的char *转换为Matlab字符串,以便可以从函数中返回它?

I accomplish this by making an opaque char* and using coder.wref to get the char**. But how do I convert the opaque char* to a Matlab string so I can return it from my function?

代码如下:

function [status,out] = cfun()
    buf = coder.opaque('char *', 'NULL');
    len = coder.opaque('size_t ', 'NULL');
    status = 0;
    status = coder.ceval('_cfun', coder.wref(buf), coder.wref(len));

    out = buf; % How do I convert buf into a Matlab string here??
end

_cfun会将一些数据写入buf,并将长度写入len.

_cfun will write some data to buf and write the length into len.

我目前可行的解决方案是在Matlab中手动创建一个char数组并将其传输到数据.像这样:

My current solution, which almost works, is to manually make a char array in Matlab and transfer to data. Like this:

length = cast(len, 'int32');
out = char(zeros(1,length+1));
for i = 1:(length+1)
    out(i) = cast(buf(i), 'int32');
end

由此产生的C代码毫无意义,并且会无限循环,但是我可以手动对其进行更新以使其正常工作.但是,每次我重新运行代码生成时,这都需要手动更新.

The C code generated from this makes no sense and loops infinitely, but I can manually update it to work. However this requires a manual update every time I rerun the code generation.

C代码如下:

i18 = length + 1L;
copyfrom = (int)i18;
i = 1;
while (i <= copyfrom) {
    source_str->data[0] = (signed char)(int)buf;
    i = 2;
}

我可以将其更新为工作状态

I can update it to work:

i18 = length + 1L;
copyfrom = (int)i18;
i = 1;
while (i <= copyfrom) {
    source_str->data[i-1] = (signed char)(int)(buf[i-1]);
    i = i+1;
}

那么,有没有一种方法可以将char * +长度转换为Matlab字符串?还是有一种更好的方式来做我想做的事情?

So, is there a way to convert the char* + length into a Matlab string? Or is there a better way of doing what I am trying to?

推荐答案

这有点浪费,但是您可以分配一个MATLAB char数组并将数据memcpy放入其中:

It's a bit wasteful, but you could allocate a MATLAB char array and memcpy the data into it:

mllen = cast(len,'int32'); % or int64
mlbuf = blanks(mllen); % allocate character vector
coder.ceval('memcpy',coder.wref(mlbuf),buf,len);
use(mlbuf);

根据len是否计数NULL终止符以及是否要使用NULL终止符,您可能需要+1或-1的大小.

You may want a +1 or -1 on sizes based upon whether or not len counts a NULL terminator and if you want it.

如果您可以在调用_cfun之前以某种方式弄清楚len,则只需将mlbuf传递给_cfun并忘记副本即可.

If you can somehow figure out len before the call to _cfun then you can just pass mlbuf to _cfun and forget the copy.

生成的代码将索引为0并产生一个无限循环,因为就Coder而言,buf是1×1 coder.opaque.如果生成并运行MEX文件,则会出现索引超出范围的错误.

The generated code is indexing with 0 and producing an infinite loop because as far as Coder is concerned, buf is a 1-by-1 coder.opaque. You would get an index out of bounds error if you generated a MEX file and ran it.

这篇关于使用Matlab Coder将C char数组转换为Matlab字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆