필터 지우기
필터 지우기

unexpected matlab operator error when plotting from a simulink matlab function

조회 수: 4 (최근 30일)
Alberto
Alberto 2023년 1월 18일
편집: Alberto 2023년 1월 19일
Hi to everyone,
I have a matlab function called from simulink and I'm trying to plot all the elements of a vector, but unexpected matlab operator error appears.
My aim is to build a vector adding [u,u] at each step (like vector=[vector;x]) and then plot it at the end of each step.
I don't know why it doesn't accept the ":" in the plot.
The code is:
function fcn(u)
coder.extrinsic('evalin', 'assignin', 'plot')
if u==0
xx=[u,u];
assignin('base','xx',xx);
sz=size(xx);
assignin('base','sz',sz);
else
sz=zeros(1,2);
szz=evalin('base','sz');
xx=zeros(szz(1),szz(2));
xxx=evalin('base','xx');
xx=[xxx;[u,u]];
assignin('base','xx',xx);
end
figure(1)
plot(xx(:,1),xx(:,2),'ko');
end
Does anyone know how to fix it?
Thanks!

답변 (1개)

Paul
Paul 2023년 1월 19일
Hi Edoardo,
Are you really trying to plot u vs. u? Wouldn't that just be a straight line?
If I understand what you're trying to do, would the XY Graph be applicable?
If wanting to do this in a Matlab Function, this code worked for me. It seems simpler and doesn't have to work with the base workspace.
function fcn(t,u)
% Incrementally add points to a line plot of t vs u
persistent init hline
if isempty(init)
init = 1;
figure;
hline = line(gca,t,u);
else
set(hline,'XData',[get(hline,'XData') t]);
set(hline,'YData',[get(hline,'YData') u]);
end
end
  댓글 수: 1
Alberto
Alberto 2023년 1월 19일
편집: Alberto 2023년 1월 19일
Hi Paul! Thanks! It works! I replaced "line" with "plot" and that's what I wanted to plot!
No, [u,u] was only an example, the vector to add changes at each step, it can be [1,0], [5,8], [4,-7] etc.

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

카테고리

Help CenterFile Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by