1

我想使用 USRP 传输编码图像。第一步是使用 Matlab 加载图像并对其进行编码。各代码如下所示。

function msg = genMsg1
%#codegen
persistent msgStrSet count;
if isempty(msgStrSet)
  count = 0;
  msgStrSet = imread('cameraman.tif'); % Load the image of cameraman.tif
end
msgtemp = dec2bin(msgStrSet); % Covert msgStrSet into binary value
msg_1ine = msgtemp(count+1,:).'; % Take msgtemp line by line and tranpose it
msg = str2num(msg_1ine);  % Convert each line from string into column vector
count = mod(count+1,65536);
end

而这个M文件的运行结果是:ans =

 1
 0
 1
 0
 0
 0
 0
 0

由于我要使用SDRU发射器的模块,所以我必须将上述代码编码成一个matlab函数,如下图所示。 在此处输入图像描述

但是当我运行这个块时,会弹出错误窗口,如下图所示。

在此处输入图像描述

第一个错误是:

Subscripting into an mxArray is not supported.
Function 'MATLAB Function' (#46.311.329), line 11, column 12:
"msgtemp(count+1,:)"
Launch diagnostic report.

第二个错误是

Undefined function or variable 'msg_1ine'. The first assignment to a local variable determines its class.
Function 'MATLAB Function' (#46.391.399), line 12, column 15:
"msg_1ine"
Launch diagnostic report.

第三个错误和第四个错误是一样的。

Errors occurred during parsing of MATLAB function 'MATLAB Function'(#45) 

我认为第二个,第三个和第四个错误是由于第一个错误,

Subscripting into an mxArray is not supported.

我在互联网上搜索了一整天,仍然找不到类似的问题。谁能告诉我到底什么是“不支持订阅 mxArray”以及如何解决它?

提前感谢任何领导!

4

2 回答 2

2

我认为不imread支持代码生成,请参阅Functions and Objects Supported for C and C++ Code Generation,因此您需要将其声明为外部。我怀疑这是你在你的块中所做的,即使你没有在你的代码中提到它。问题是当函数被声明为外部函数时,它返回的数据类型是mxArray,请参阅调用 MATLAB 函数,尤其是“使用 mxArrays”部分。

解决方法是将msgStrSet变量初始化为 0,以强制 MATLAB Coder 将变量数据类型设置为double(或除 之外的任何内容mxArray):

function msg = genMsg1
%#codegen
coder.extrinsic('imread'); % declare imread as extrinsic
persistent msgStrSet count;
if isempty(msgStrSet)
  count = 0;
  msgStrSet = 0; % define msgStrSet as a double
  msgStrSet = imread('cameraman.tif'); % Load the image of cameraman.tif
end
msgtemp = dec2bin(msgStrSet); % Covert msgStrSet into binary value
msg_1ine = msgtemp(count+1,:).'; % Take msgtemp line by line and tranpose it
msg = str2num(msg_1ine);  % Convert each line from string into column vector
count = mod(count+1,65536);
end
于 2014-09-29T09:33:41.247 回答
0

谢谢您的帮助。正确的代码写如下。

function msg = genmsg
persistent count msgStrSet;
coder.extrinsic('imread','str2num');
if isempty(msgStrSet)
    count = 0;
    msgStrSet = zeros(256,256,'uint8'); % Intialize msgStrSet as unsigned interger matrix of 256*256
    msgSet = imread('cameraman.tif'); % Load the image of cameraman.tif 
end
msgtemp = dec2bin(msgStrSet);  % Covert msgStrSet into binary value
msg_line = msgtemp(count+1,:).' % Take msgtemp line by line and tranpose it
msg = zeros(8,1,'double'); % Intialize msg as matrix of 8*1.
msg = str2num(msg_line); % Convert each line from string into column vector
count = mod(count+1,65536);
end
于 2014-10-01T12:51:20.797 回答