Adding legends for a plot in matlab function block.

i wanted to add a legend to the plot which is the output from matlab function block, but im ending up with an error as "Function 'legend' not supported for code generation." so can anyone help me out to add this legend function in matlab function block.
function [mean, stdev] = stats(vals)
len = length(vals);
mean = avg(vals,len);
stdev = sqrt(sum(((vals-avg(vals,len)).^2))/len);
plot(vals,"-+");
legend("Autoupdate","off")
end

4 comentarios

Adam Danz
Adam Danz el 18 de En. de 2024
What is avg? Did you intend to compute the average or mean?
Charan
Charan el 19 de En. de 2024
i am calculating the mean over here , but anyways my main point is to have a legend for a plot which is plotted from matlab function.
Right after receiving this error, could you run the following 2 lines of code and share the content each line produces? Also, please let me now what MATLAB release you're using.
S = lasterror
S.stack
The error is reported by Simulink during model compile. The code has not been able to be executed yet.

Iniciar sesión para comentar.

Respuestas (1)

Fangjun Jiang
Fangjun Jiang el 18 de En. de 2024
Editada: Fangjun Jiang el 18 de En. de 2024
There is a way to deal with this type of error.
However, are you sure you want to do this? Because the MATLAB Function block in a Simulink model usually is executed at every simulation step.
This recent Q&A might give you a better idea.

1 comentario

Charan
Charan el 23 de En. de 2024
Movida: Fangjun Jiang el 23 de En. de 2024
thanks for the answer , i was able to solve the above error by passing the legend as an extrinsic function inside a matlab function block.
coder.extrinsic('legend')

Iniciar sesión para comentar.

Preguntada:

el 18 de En. de 2024

Movida:

el 23 de En. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by