help for using simulink and usrp

조회 수: 1 (최근 30일)
Volkan
Volkan 2013년 2월 5일
I want transmit an image using Simulink and USRP. I have the following code
%if true
function msg = genMsg
persistent imgmatRows imgmatCols imgvec msgBin msgBinRows msgBinCols msgTmp;
coder.extrinsic('imread');
if isempty(imgvec)
imgmat = imread('cameraman.tif');
[imgmatRows, imgmatCols] = size(imgmat);
imgvec = reshape(imgmat, 1, imgmatRows * imgmatCols);
msgBin = de2bi(int8(imgvec), 7, 'left-msb');
[msgBinRows, msgBinCols] = size(msgBin);
msgTmp = reshape(double(msgBin).', msgBinRows*msgBinCols, 1);
end
msg = msgTmp;
When I run this code it produces the following error message:
"Function output 'msg' cannot be an mxArray in this context. Consider preinitializing the output variable with a known type."
How can I fix this problem?

채택된 답변

Ryan G
Ryan G 2013년 2월 5일
You are calling imread extrinsically and it will return a variable of type mxArray back to the MATLAB function. This cannot be done since Simulink cannot use mxArray datatypes.
To fix this, define imgmat as a double before calling imread like this:
imgmat = zeros(pixelwidth,pixelheight,cdata);
imgmat = imread('cameraman.tif');
That will force the coder to generate imgmat as a type double. When you call imread it will store the result in a double. You will also need to know the size of the image before running this as the result will be placed in a defined matrix. I would run the imread in MATLAB first to determine what imgmat should be defined as.
  댓글 수: 2
Volkan
Volkan 2013년 2월 7일
Hi Ryan, I did the changes you mentioned in the answer but after that another error was occured. New error is:
Data 'msg' (#24) is inferred as a variable size matrix, while its specified type is something else.
How can I fix this?
Ryan G
Ryan G 2013년 2월 7일
There are 2 things you can do here.
1) Define msg like you did with imgmat. In other words, you need to make msg a non-variable size matrix. That is a limitation of simulink compared to matlab as it will generate c-code for this function and c does not handle the variable sized matrix like MATLAB.
2) Since you are already calling part of the function extrinsically, you could go ahead and write the entire contents to a function and call the whole thing extrinsically. This would save you that hassle of defining imgmat and you would only need to define msg. The downside is your model will run slower and you can't generate code.
However, you can't generate code anyway right now (since imread is extrinsic) and the slowdown may be negligible for your application.
Of course an alternative would involve computer vision system toolbox as it would allow you to import the image directly.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Communications Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by