How do I plot a X-Y graph in matlab with data using "to workspace" block
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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)
Accepted Answer
Ameer Hamza
on 5 Jun 2020
Something like this
plot(out.xout{8}.Data, out.yout{8}.Data)
13 Comments
Geoffrey Lo
on 5 Jun 2020
I tried and it has following:
Error using to_workspace_file (line 28)
Brace indexing is not supported for variables of this type.
Ameer Hamza
on 5 Jun 2020
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);
Geoffrey Lo
on 5 Jun 2020
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')
Ameer Hamza
on 5 Jun 2020
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)
Geoffrey Lo
on 5 Jun 2020
Is there another method, cause the output is suppose to be circle, but instead it turn out to be something like below

Ameer Hamza
on 5 Jun 2020
Can you attach the variables out1 and out2 in a .mat file?
Geoffrey Lo
on 5 Jun 2020
Ameer Hamza
on 5 Jun 2020
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
on 5 Jun 2020
Edited: Geoffrey Lo
on 5 Jun 2020
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.
Ameer Hamza
on 6 Jun 2020
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.
Geoffrey Lo
on 6 Jun 2020
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
Ameer Hamza
on 6 Jun 2020
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.
Geoffrey Lo
on 6 Jun 2020
Thanks a lot, it works now :)
More Answers (0)
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)