trapz errors in compiled code but not script

3 visualizzazioni (ultimi 30 giorni)
matthew
matthew il 28 Dic 2018
Commentato: matthew il 5 Gen 2019
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.

Risposte (1)

Naman Chaturvedi
Naman Chaturvedi il 2 Gen 2019
Modificato: madhan ravi il 2 Gen 2019
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 Commento
matthew
matthew il 5 Gen 2019
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()).

Accedi per commentare.

Prodotti


Release

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by