UNIX Matlab - Calculate plot in loop, but show at the end - matlab

I am plotting stem plots in given figures within a function using the following code...
% plot - phase = 1,2 or 3, with different data each time
% Each phase is called more than once
figure(phase);
stem(1:length(phaseSystem),phaseFailureTimes);hold on
This function is called several times within a loop, plotting on the same figures iteratively.
I want each plot to be calculated but not shown until a later time. This is because the figures currently show and update live, which is slowing down the script. I'd rather calculate but hide them, as opposed to storing all the data and plotting them at the end.
Thanks

I think you need a bit of reorganizing so that your function outputs the 3 values for your variables phaseSystem and phaseFailureTimes as n by 3 matrices.
Let me call this function calculate_phase_failure. Then the script/function that calls calculate_phase_failure could accumulate the results. Finally, a separate loop at the end could generate your plots. If the number of elements are different for each iteration of your loops you might need to use a cell-array to accumulate your results.
Here is an example for the simplest case where the number of elements is consistent between iteration of your loops.
for i=1:n
[phaseSystem(:,:,i), phaseFailureTime(:,:,i)] = calculate_phase_failure( <input variables> );
end
% now generate your plot
for i=1:n
for phase=1:size(phaseFailureTime,2)
figure(phase);
stem(1:size(phaseSystem,1), phaseFailureTime(:,phase,i))
end
end

Related

How do I plot inside a for loop? Matlab

I'm trying to plot a straight line from a point in x to different values of t, thereby making a line in a for loop. But I see no lines generated in my figure in MATLAB
Following is my code:
t=linspace(0,8,11)
xs=(1.+t).^0.5
x0=xs./(1.+t)
m=size(t)
n=max(m)
hold on
for k=1:n
plot(x0(k),t(1:k),'-')
hold on
end
Thanks
You do not need the loop to perform the plot.
plot(x0,t,'-')
Will work just fine! Unless you were attempting to plot points...use scatter() for that:
scatter(x0,t)
plot() and scatter() (and most of Matlab's functions) are meant to be used with vectors, which can take some time to get used to if you are used to traditional programming languages. Just as you didn't need a loop to create the vector x0, you don't need a loop to use plot().
You are adding one point in Y axis along a line in X Axis use this code
t=linspace(0,8,11)
xs=(1.+t).^0.5
x0=xs./(1.+t)
m=size(t)
n=max(m)
hold on
for k=1:n
plot(x0(1:k),t(1:k),'-')
hold on
end
for more fun and see exactly how for is performed use this for loop
for k=1:n
pause('on')
plot(x0(1:k),t(1:k),'-')
hold on
pause(2)
end

Select and plot value above a threshold

I have a plot in which there are a few noise components. I am planning to select data from that plot preferably above a threshold in my case I am planning to keep it at 2.009 on the Y axis. And plot the lines going only above it. And if anything is below that i would want to plot it as 0.
as we can see in the figure
t1=t(1:length(t)/5);
t2=t(length(t)/5+1:2*length(t)/5);
t3=t(2*length(t)/5+1:3*length(t)/5);
t4=t(3*length(t)/5+1:4*length(t)/5);
t5=t(4*length(t)/5+1:end);
X=(length(prcdata(:,4))/5);
a = U(1 : X);
b = U(X+1: 2*X);
c = U(2*X+1 : 3*X);
d = U(3*X+1 : 4*X);
e = U(4*X+1 : 5*X);
figure;
subplot (3,2,2)
plot(t1,a);
subplot (3,2,3)
plot(t2,b);
subplot(3,2,4)
plot(t3,c);
subplot(3,2,5)
plot(t4,d);
subplot(3,2,6)
plot(t5,e);
subplot(3,2,1)
plot(t,prcdata(:,5));
figure;
A=a(a>2.009,:);
plot (t1,A);
This code splits the data (in the image into 5 every 2.8 seconds, I am planning to use the thresholding in first 2.8 seconds. Also I had another code but I am just not sure if it works as it took a long time to be analysed
figure;
A=a(a>2.009,:);
plot (t1,A);
for k=1:length(a)
if a(k)>2.009
plot(t1,a(k)), hold on
else
plot(t1,0), hold on
end
end
hold off
The problem is that you are trying to plot potentially several thousand times and adding thousands of points onto a plot which causes severe memory and graphical issues on your computer. One thing you can do is pre process all of the information and then plot it all at once which will take significantly less time.
figure
threshold = 2.009;
A=a>threshold; %Finds all locations where the vector is above your threshold
plot_vals = a.*A; %multiplies by logical vector, this sets invalid values to 0 and leaves valid values untouched
plot(t1,plot_vals)
Because MATLAB is a highly vectorized language, this format will not only be faster to compute due to a lack of for loops, it is also much less intensive on your computer as the graphics engine does not need to process thousands of points individually.
The way MATLAB handles plots is with handles to each line. When you plot a vector, MATLAB is able to simply store the vector in one address and call it once when plotting. However, when each point is called individually, MATLAB has to store each point in a separate location in memory and call all of them individually and graphically handle each point completely separately.
Per request here is the edit
plot(t1(A),plot_vals(A))

How to plot inside while-loop in MATLAB?

Inside a while loop, I have some function that creates all the neccesary y-values for the plot I want to make. After all the y-values are done I want my program to plot the dat(while still inside the loop), but the plot can't be made because the data won't come out until the end of the loop.
Is there anyway to do this?
Basically my code is(and I'm just going to for the first case here)
while c~=3
c=menu('a','b','c')
switch c
case 1
for
%function that creates y-values
end
plot(x,y)
end
end
As I said; I get out all the data at the end of the loop, which is stored in the workspace. Meaning that when I run it a second time, it works fine.
But I want to know how to make it work the first time.
for Continuous line plot you can use drawnow and here it is explained how to do this (remember to use pause(.) if you want to visualize the changes "real-time".
for retain current plot when adding new plots use hold on as it is explained here
if you want to open different windows for every different plot you can use something like:
ii=1;
while ...
...
figure(ii)
plot(x,y)
ii=ii+1;
...
end
but be careful with the last one: if you have a big number of plots you can have some problem

How do I plot curves of a sequence of functions indexed by i

I was trying to plot the graphs in the interval [60, 110] of a sequence of ten functions f_i(t) in the same figure, whose definitions will be clear from the code below:
figure
i=1;
while i<=10;
P_i=abs(sin(i));
r_i=0.005*abs(cos(i.^2));
y=#(i,t)P_i*exp(r_i*t)/(1+P_i*(exp(r_i*t)-1));
% disp([(y(i,67));(y(i,68));(y(i,69))]');
s=linspace(60,110,51);
i=i+1;
continue;
end;
I ran the piece of code and it works. As you see, I could create one single function y(i,t) where i is an integer 1<=i<=10, and t is a continuous variable. But how can I construct ten functions y_i of a single variable t in the above code and plot their graphs in the same figure, so it'll consist of the graphs of ten functions together. How can I achieve it?
Thanks
First of all some minor modifications on your code. P_i and r_i are functions as well, so define them as functions. This way you only need to define the function once. Besides using a loop as you did it easily causes errors. i is the imaginary unit and you did not initialise it. Use a for loop instead and avoid i
P_i=#(i)abs(sin(i));
r_i=#(i)0.005*abs(cos(i.^2));
k=1:10;
t=60:101;
y=#(i,t)P_i(i)*exp(r_i(i)*t)/(1+P_i(i)*(exp(r_i(i)*t)-1));
To plot a Matrix with all values is required:
M=nan(numel(ik),numel(it));
for ik=1:numel(k)
for it=1:numel(t)
M(ik,it)=y(k(kx),t(it));
end
end
And finally Plot it. I did not really understand if you want plot(k,M) or plot(t,M) but one should be the right.

saving the matrix and the plot figures

I have a matlab program that generate a matrix f (which is 3 column and 100 raw) then the program plot the 1st column with second one and third one
every time I change a specific variable the matrix values changes and the plot changes
if I made a for loop to generate different matrices with different plots how could I made the program to save the matrix and the plot figures each loop with different names in the same folder
You can save the matrices inside the loop or place them in a cell array and save that after the loop. To save figures to file you can use print.
Something along these lines:
for ii=foo:bar
mat=...;
fig=figure;
% somehow create matrix mat and figure fig;
save(['quux',num2str(ii),'.mat'],mat);
print(fig,['quux',num2str(ii)]);
end