How do I plot a X-Y graph in matlab with data using "to workspace" block

I have 2 simulations and I took the data from both simulations and the data I wants is at out.xout{8} and out.yout{8} as a 1x1 double timeseries. How do I plot a X-Y graph out of these data? The code for retrieving data from "to workspace" block is as below:
clear all
close all
clc
%open system
open_system('circlex_ds');
%set parameters for x to workspace
set_param('circlex_ds/To Workspace',...
'VariableName','x')
%output the simulation results
out = sim('circlex_ds');
%plot function
figure()
plot(out.x)
x=out.xout{8};
%open system
open_system('circley_ds');
%set parameters for y to workspaae and for file
set_param('circley_ds/To Workspace',...
'VariableName','y')
%output the simulation results
out = sim('circley_ds');
%plot function
figure()
plot(out.y)

 채택된 답변

Something like this
plot(out.xout{8}.Data, out.yout{8}.Data)

댓글 수: 13

I tried and it has following:
Error using to_workspace_file (line 28)
Brace indexing is not supported for variables of this type.
Is out.xout a time series? Maybe try this:
Change line to
out1 = sim('circlex_ds');
and
out2 = sim('circley_ds');
and then try
plot(out1.x.Data, out2.y.Data);
I changed and had following:
Error using plot
Vectors must be the same length.
Error in to_workspace_file (line 28)
plot(out1.x.Data, out2.y.Data,'same')
It shows both have a different number of elements. One solution is to use interpolation so that they have the same number of elements
x = out1.x.Data;
y = out2.y.Data;
y_interp = interp1(1:numel(y), y, linspace(1, numel(y), numel(x)));
plot(x, y_interp)
Is there another method, cause the output is suppose to be circle, but instead it turn out to be something like below
Can you attach the variables out1 and out2 in a .mat file?
Here are the out1 and out where out 1 is x and out is y
There might be some problem with your simulation results. The variable you shared does not appear to be creating a circle. To create a circle, 'x' and 'y' must be sinusoidal signals individually, with a phase shift of 90 degrees. You can see that this is not the case with the data you shared.
Geoffrey Lo
Geoffrey Lo 2020년 6월 5일
편집: Geoffrey Lo 2020년 6월 5일
It should be a circle as I verified with the matlab code below and plot(out1.x) and plot(out2.y) just give the same results as the code.
If you plot out1.x.Data and then plot 'x' in your last script, you will see that they are not the same. Therefore, the Simulink output does not create a circle.
but when I use plot(out.x) and plot(out.y), it is the same as the x and y equation from circle_code_m
Oh! I forgot to consider that the time vector might not be equally spaced. Following will work
Time = linspace(0, 7, 1000);
x_interp = interp1(out1.x.Time, out1.x.Data, Time);
y_interp = interp1(out2.y.Time, out2.y.Data, Time);
plot(x_interp, y_interp)
it uses out1 and out2 you shared preiously.
Thanks a lot, it works now :)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2020년 6월 5일

댓글:

2020년 6월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by