MATLAB: Plotting ScopeData on Workspace - matlab

I configured the data from Simulink to be saved as Structure with time on workspace, but I'm getting an error when I try to plot the variable.
plot(X.time, X.signals.values)
X is how I named the scope data.
And this is the error I get
Error using plot
Data must be a single matrix Y or a list of pairs X,Y.
Is there some other configuration I should make on Simulink for this to work?
I have the 2017 version of Matlab

Related

Demux vector from workspace

I have vector defined in workspace. I would like to get every element in vector to different inputs in my simulink model. I can't demux vector. Does anyone know how to do it?

Why is only one plot exponential and the rest are linear in matlab? (ODE15s)

I'm using ODE15s function on Matlab 2018 to model an epidemic (SIR model). The output should show the population in three groups over time: Susceptible, Infected, and Recovered. The following equations are what are used by epidemiologists to describe the recovery model. This code is in a separate file that will be used in the main file.
function dYdt=EpiDiff(t,Y)
%dYdt=zeros(3,1);
i=10^5; %infection rate
r=0.05; %recovery rate
b=5e-5; %birth rate
S=Y(1);
Inf=Y(2);
R=Y(3);
dYdt(1)=(i*Inf*S)+(r*Inf);
dYdt(2)=-(i*Inf*S)+(b*(Inf+S+R));
dYdt(3)=r*Inf;
dYdt=[dYdt(1);dYdt(2);dYdt(3)];
%If this final line isn't included, an error occurs about the matrix size.
end
Then the main code has the ode15s code that uses the function. I initially used ode45 (recommended it try first) but it was too stiff.
options=odeset('OutputFcn','odeplot','Outputsel',[0,45000]);
[t,Y]=ode15s('EpiDiff',linspace(t_0,t_final,n_points),[300 30000 400000]);
hold on
plot(t,Y(:,1),'-')
plot(t,Y(:,2),'-')
plot(t,Y(:,3),'-')
title('Population against Time (Days)')
legend('Susceptibles','Infected','Recovered');
xlabel('Time (Days)')
ylabel('Population')
hold off
When using the plot function, the following occurs:
Link to graph
Why does the other two plots show up as a straight line? They're supposed to exponential, not linear...

How to convert double to object handle to graph a contour plot?

I am working with app designer in MATLAB where I have the user input a bunch of parameters and push a button and then generate a contour plot. The app first uses dlmread to save data from 3 separate files to the workspace. Then what the goal is, is to generate a corresponding contour plot on that same GUI that uses data from those 3 files (my x, y, and z parameters).
However, when I run the program, I get an error that says:
"Error setting property 'HetroTransSpec' of class 'Parameters':
Cannot convert double value 5 to a handle"
app.HetroTransSpec is the name of my contour plot. Parameters is the name of my GUI application. I will present the code:
function SetParametersButtonPushed(app, event)
spec = dlmread('/Users/******/MATLAB/SphoHetroTest/Spec3.txt'); %Load spec file
assignin('base', 'spec', spec);
pop=dlmread('/Users/******/MATLAB/SphoHetroTest/pop.txt');
assignin('base', 'pop', pop);
lambda=dlmread('/Users/******/MATLAB/SphoHetroTest/lambda.txt'); %read wavelenght axis (nm)
assignin('base', 'lambda', lambda);
Now, here is my code to take these parameters (spec, pop, lambda) to generate my contour plot. Except I am getting that error:
app.HetroTransSpec = contourf(pop,lambda,spec);
Any help would be greatly appreciated!
The result of a contour plot is not a handle, but contour data, as opposed to some other plotting functions.
Try:
[~,handle]= contourf(pop,lambda,spec);
app.HetroTransSpec =handle;

Exporting signal data from figure in Matlab

Is it somehow possible to get signal data from figure, to save the vector or matrix of the data to the Workspace?
We happend to make a lots of measurements on a real system in school, but ve saved only figures of the measutrement and now we need to use some of the signals from the figure and use them in another figure for comparison.
You can load the figure in Matlab and go to View->Properties, to pull the data out of the plot's properties e.g. for a line graph plot:
You can get at the XData and YData properties and copy/paste the values of out it e.g.
Alternatively, as I had to do once when this method failed, you can save the figure as EPS/postscript and try to pull the data out of the postscript file in a text editor (!)

Matlab Simulink graph plotting

I plotted a signal using "To Workspace" in Simulink Matlab. Now I want to take the mean of the specific part of that signal which I plotted.How can I extract values from "To Workspace" or how can I take the mean of the specific area of that graph.
In "To workspace" you define a variable name, let's say: "simout"
I made a simple simulink as the following:
you can save with different formats: Timeseries, Structure with time, Structure, Array.
Then, when you run the simulink, it will save the variable in the worksapce as a structure.
Then you can use the variable to plot the data inside. check this example :
consider you saved using Structure with time you can get data like this:
t = simout.time
x = simout.signals.values
and you can plot the data:
plot(t,x)