trapz errors in compiled code but not script

조회 수: 3 (최근 30일)
matthew
matthew 2018년 12월 28일
댓글: matthew 2019년 1월 5일
I have to compile and run my Matlab code as an executable but I'm running into an issue that I haven't found a solution to. My code uses trapz(), and it works without problems when I test my code by running the script, it compiles without error/warning, but when I run the compiled code, I get a failure.
dt=0.25; %sec
tempData=x.^2; %vector of squared values
tempData=tempData(:)'; %ensure 1xN vector
meanPower=trapz(dt,tempData)/(stop_time-start_time);
The error that I recieved was:
trapz error LENGTH(X) must equal the length of Y in dim 1.
size(x)=1x1. size(y)=1xN, so this should work, and, in fact, it does when I just run the code in uncompiled form. I also tried using a transposed tempData (size=Nx1), but that resulted in a variant of the error:
trapz error LENGTH(X) must equal the length of Y in dim 2.
There appears to be an error with using trapz() in compiled (mcc) form, but I haven't come across a page discussing this error or how to get around it.
I made a local copy of trapz, calling it Mytrapz() and adjusted the code above to call it instead of trapz(). I added several disp() comments into Mytrapz to try to figure out where it was breaking down. I add Mytrapz() as a subfunction in the m-file containing my my function. I then re-compiled my code and ran it. I made no other changes to trapz() (just disp() commands to tell me how it was progressing). Now the compiled code runs.
My question is: why does the code run now that I took an exact copy of trapz() and put it in my primary m-file whereas trapz(), when called from my primary m-file produced an error? I'm grateful that it's running, but I don't understand why.

답변 (1개)

Naman Chaturvedi
Naman Chaturvedi 2019년 1월 2일
편집: madhan ravi 2019년 1월 2일
From the documentation of
Q = trapz(X,Y) integrates Y with respect to the coordinates or scalar spacing specified by X.
  • If X is a vector of coordinates, then length(X) must be equal to the size of the first dimension of Y whose size does not equal 1.
  • If X is a scalar spacing, then trapz(X,Y) is equivalent to X*trapz(Y).
So, if your function call, as per your workflow would look like:
>> meanPower=trapz(x_start:dt:x_stop,tempData)/(stop_time-start_time);
HTH
  댓글 수: 1
matthew
matthew 2019년 1월 5일
I still don't understand why the compiled matlab code that called trapz() failed, but 1) worked when it was run as a script and 2) worked in the compiled version only after I copied trapz() in as a new subfunction (mytrapz()).

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

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by