问题
I would like to transmit an encoded image using USRP. The first step is to load the image using Matlab and encode it. The respective codes are shown as follows.
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
And the running result of this M-file is: ans =
1
0
1
0
0
0
0
0
Since I should use the block of SDRU transmitter, I have to encode above codes into a matlab function, which is shows as following figure.
But when I run this block, error windows pops up, as the following figure shows.
The first error is:
Subscripting into an mxArray is not supported.
Function 'MATLAB Function' (#46.311.329), line 11, column 12:
"msgtemp(count+1,:)"
Launch diagnostic report.
The second error is
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.
The third error and fourth error are the same.
Errors occurred during parsing of MATLAB function 'MATLAB Function'(#45)
I think the second, third and fourth errors are due to the first error,
Subscripting into an mxArray is not supported.
I have search the internet for the whole day, and still could not find the similar problem. Could anyone tell me what is "Subscripting into an mxArray is not supported" after all and how to solve it?
Thanks in advance for any leading!
回答1:
I don't think imread
is supported for code generation, see Functions and Objects Supported for C and C++ Code Generation, so you need to declare it as extrinsic. I suspect this is what you have done in your block, even though you don't mention it in your code. The problem is when a function is declared as extrinsic, the data type it returns is mxArray
, see Call MATLAB Functions, and in particular the "Working with mxArrays" section.
The workaround is to initialise your msgStrSet
variable to 0, to force MATLAB Coder to set the variable data type to double
(or anything other than 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
回答2:
Thanks for the help. The correct codes are written as follows.
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
来源:https://stackoverflow.com/questions/26096243/get-errors-when-running-simulink-block-but-get-success-when-running-respective