how i can fix matlab function block coder.extrinsic issue ?

I am usign MATlab function block in my simulation model as relay such as
function y = fcn(P,T)
if(T <= 12.0)
y=1;
else
if (P <= 4800000)
y=0;
else
y=1;
end
end
and it is working well for my model
but I want to use mean of 5 maximum peaks value in a specific interval such as
function y = fcn(P,T)
if(T <= 12.0)
y=1;
else
if((mean(maxk(findpeaks(P((T >= 12) & (T <= 12.1))),5))) <= 4800000)
y=0;
else
y=1;
end
end
but when I use this command
(mean(maxk(findpeaks(P((T >= 12) & (T <= 12.1))),5)))
in my matlab function then following error appears
Function 'maxk' is not supported for code generation. Consider adding coder.extrinsic('maxk') at the top of the function to bypass code generation
where as this command is forking well in command window, I don’t know why this error appearing and how I can fix it
I need help regarding this issue

Antworten (1)

Walter Roberson
Walter Roberson am 8 Apr. 2019
Right after the function line, add the line
coder.extrinsic('maxk');
Note: you will not be able to run your code with any acceleration turned on.
In order to be able to run your code with accelation turned on, you will need to convert to using sort() and extracting the values you are interested in.

21 Kommentare

i have written like this
function y = fcn(P,T)
coder.extrinsic('maxk');
if(T <= 12.0)
y=1;
else
if ((mean(maxk(findpeaks(P((T >= 12) & (T <= 12.1))),5))) <= 4800000)
y=0;
else
y=1;
end
end
and then following error is appearing
Expected either a logical, char, int, fi, single, or double. Found an mxArray. MxArrays are returned from calls to the MATLAB interpreter and are not supported inside expressions. They may only be used on the right-hand side of assignments and as arguments to extrinsic functions.
You need to predefine a variable with zeros(1,5), and assign the output of the maxk(expression,5) to that variable. Then you can do the mean() calculation on that variable and so on.
i have added extrinsic like this
function y = fcn(P,T)
coder.extrinsic('x');
x=(mean(max(findpeaks(P((T >= 12) & (T <= 12.1))),5)));
if(T <= 12.0)
y=1;
else
if (x <= 4800000)
y=0;
else
y=1;
end
end
but now following error is appearing
An error occurred while running the simulation and the simulation was terminated
Caused by:
  • Expected Y to be nonempty.
why this error appear and how i can reslove this
function y = fcn(P,T)
coder.extrinsic('maxk');
maxk_result = zeros(1,5);
if (T <= 12.0)
y=1;
else
maxk_result = maxk(findpeaks(P((T >= 12) & (T <= 12.1))),5);
if ((mean(maxk_result)) <= 4800000)
y=0;
else
y=1;
end
end
However I would tend to expect that it will now complain about findpeaks().
Are you expecting your T to be a scalar or a vector? What output are you expecting if T > 12.1 ?
i have also tried this
function y = fcn(P,T)
coder.extrinsic('maxk');
R=zeros(5,1);
R=(maxk(findpeaks(P((T >= 12) & (T <= 12.1))),5));
if(T <= 12.0)
y=1;
else
if (mean(R) <= 4800000)
y=0;
else
y=1;
end
end
but same error
An error occurred while running the simulation and the simulation was terminated
Caused by:
  • Expected Y to be nonempty.
You need to defer the R=(maxk...) to after the else. Otherwise when T < 12, you are calling findpeaks with P(false) which is empty.
The code I posted uses the right order, but would still have problems if T > 12.1. I asked you what should be done for that case, but you did not happen to respond.
and m sorry, i haven't refresh the page so i was unable to see your previous reply, i have just seen it.
actually my code should have to decide within this duration T>12 & T<12.1
if during this duration ( T>12 & T<12.1) corresponding value of (maxk(findpeaks(P((T >= 12) & (T <= 12.1))),5)) is less than 4800000 then y=0 and if it is greater than 4800000 then y=1, for the case T > 12.1, "Y" should follow the decision which is made during the duration (T>12 & T < 12.1)
the code u have written is working properly, but at T=12 again follwoing error has occured
An error occurred while running the simulation and the simulation was terminated
Caused by:
  • Data set must contain at least 3 samples
What is the structure of P ? Is it a static 2D array that has rows corresponding to time? Is it a changing vector all of which should be put through findpeaks provided the time is 12.0 to 12.1 ?
function y = fcn(P,T)
persistent prev_y
if isempty(prev_y)
prev_y = 1;
end
coder.extrinsic('maxk');
R = zeros(1,5);
if T <= 12.0
y=1;
prev_y = y;
elseif T > 12.1
y = prev_y;
else
R = maxk(findpeaks(P),5);
y = double(mean(R) <= 4800000); %convert logical to numeric
prev_y = y;
end
i am exporting scope data to workspcae and its format is structrue with time
yes P is 2 D array with time
latest code errors
Data set must contain at least 3 samples. Function 'MM Operator & Trip Signal Generator' (#23.208.220), line 14, column 13: "findpeaks(P)" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'R'. The first assignment to a local variable determines its class. Function 'MM Operator & Trip Signal Generator' (#23.244.245), line 15, column 20: "R" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Errors occurred during parsing of MATLAB function 'MM Operator & Trip Signal Generator'
Component:MATLAB Function | Category:Coder error
Errors occurred during parsing of MATLAB function 'MM Operator & Trip Signal Generator'
function y = fcn(P,T)
coder.extrinsic('maxk');
maxk_result = zeros(1,5);
if (T <= 12.0)
y=1;
else
maxk_result = maxk(findpeaks(P((T >= 12) & (T <= 12.1))),5);
if ((mean(maxk_result)) <= 4800000)
y=0;
else
y=1;
end
end
it was working well till T=12 but after T=12 following error appear
Data set must contain at least 3 samples.
Is the first column of P time? Or the first row? That is, does a particular row or column of P need to be selected based upon the current time?
Or is the original P coming from a From File Block or something like that? Because if it is, then what you would receive at any time is probably going to be a vector representing the (interpolated) current value of P.
It is not obvious to me how R could be undefined. Did you copy that most recent version of mine, the one with the persistent variable ?
time and P coloumn
P and Time from scope are as i have share
It looks to me as if your P is a timeseries. If so then at any time, the number of values that would be passed in through the P parameter would be the same as the number of columns of Scope2.signals.values, which is to say only 1. At the moment I do not understand what you would be findpeaks() of.
However, if the code before was working for you up to time 12.1, then try the code I posted that uses persistent
that code was working till T=12
i have also tried this but it also stops at T=12
function y = fcn(P,T)
coder.extrinsic('maxk');
T_int=[12,12.1];
idx = find((T >= T_int(1)) & (T <= T_int(2)));
maxk_result = zeros(1,5);
if (T <= 12.0)
y=1;
else
maxk_result = maxk(findpeaks(P(idx)),5);
if ((mean(maxk_result)) <= 4800000)
y=0;
else
y=1;
end
end
An error occurred while running the simulation and the simulation was terminated
Caused by:
  • Data set must contain at least 3 samples
Before T = 12, you are hitting the T <= 12.0 condition that does not require examining the input.
After T = 12, you start to try to use the contents of the P input. However, as I explained to you before, your P is a timeseries, and since it only has one column, you are only going to be passed one value of P at any given time -- the P appropriate for that time (either interpolated or held from the previous time that was explicitly in the data.)
then what is the solution for this? what changes should i do in my code?
You want to examine the peaks of something that is a single reading.
  • You could give up on the idea of "peaks" and simply compare the single value in P to 4800000 .
  • You could use a multi-tap delay line or memory buffer to hold on to a certain number of recent samples, and examine the peaks of those
  • perhaps you could give up on the peaks part, and instead integrate your signal, and subtract from that the integral of the signal taken at an earlier time (and held through a delay line or something similar), and divide by the time difference, to get a mean of the entire signal over that time (there might well be other ways to create a "moving mean" block.) In context, perhaps that moving mean might be good enough for your purposes. It depends: is your signal prone to sharp narrow large spikes and that is why you are looking for peaks, or are you looking for the case where the signal value has become overall large for a while ?
  • perhaps in context it would make sense to transform your timeseries into a constant block where the entire past and future history is available, and have your MATLAB Function Block somehow select from that based on time. Remember, though, that the function block is being invoked repeatedly, always for a specific time, not for a range of times.

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 8 Apr. 2019

Kommentiert:

am 13 Apr. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by