运行Simulink块时获取错误,但仅在运行相应的M文件时获得成功

时间:2014-09-29 09:08:40

标签: matlab simulink matlab-coder

我想使用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函数,如下图所示。 enter image description here

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

enter image description here

第一个错误是:

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"毕竟以及如何解决它?

提前感谢任何领导!

2 个答案:

答案 0 :(得分:2)

我认为代码生成不支持imread,请参阅Functions and Objects Supported for C and C++ Code Generation,因此您需要将其声明为外在的。我怀疑这是你在你的块中所做的,即使你没有在你的代码中提到它。问题是当一个函数被声明为外在函数时,它返回的数据类型是mxArray,请参阅Call MATLAB Functions,特别是"使用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

答案 1 :(得分: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