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
Related
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
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
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.
This is about how MATLAB can take very different times to plot the same thing — and why.
I generate 10000 points in 3D space:
X = rand(10000, 1);
Y = rand(10000, 1);
Z = rand(10000, 1);
I then used one of four different methods to plot this, to create a plot like so:
I closed all figures and cleared the workspace between each run to try to ensure fairness.
Bulk plotting using scatter3:
>> tic; scatter3(X, Y, Z); drawnow; toc
Elapsed time is 0.815450 seconds.
Individual plotting using scatter3:
>> tic; hold on;
for i = 1:10000
scatter3(X(i), Y(i), Z(i), 'b');
end
hold off; drawnow; toc
Elapsed time is 51.469547 seconds.
Bulk plotting using plot3:
>> tic; plot3(X, Y, Z, 'o'); drawnow; toc
Elapsed time is 0.153480 seconds.
Individual plotting using plot3:
>> tic; hold on
for i = 1:10000
plot3(X(i), Y(i), Z(i), 'o');
end
drawnow; toc
Elapsed time is 5.854662 seconds.
What is it that MATLAB does behind the scenes in the 'longer' routines to take so long? What are the advantages and disadvantages of using each method?
Edit:
Thanks to advice from Ben Voigt (see answers), I have included drawnow commands in the timing — but this has made little difference to the times.
The main difference between the time required to run scatter3 and plot3 comes from the fact that plot3 is compiled, while scatter3 is interpreted (as you'll see when you edit the functions). If scatter3 was compiled as well, the speed difference would be small.
The main difference between the time required to plot in a loop versus plotting in one go is that you add the handle to the plot as a child to the axes (have a look at the output of get(gca,'Children')), and you're thus growing a complicated array inside a loop, which we all know to be slow. Furthermore, you're calling several functions often instead of just once and incur thus calls from the function overhead.
Recalculation of axes limits aren't an issue here. If you run
for i = 1:10000
plot3(X(i), Y(i), Z(i), 'o');
drawnow;
end
which forces Matlab to update the figure at every iteration (and which is A LOT slower), you'll see that the axes limits don't change at all (since the default axes limits are 0 and 1). However, even if the axes limits started out differently, it wouldn't take many iterations for them to converge with these data. Compare with omitting the hold on, which makes plotting take longer, because axes are recalculated at every step.
Why have these different functions? scatter3 allows you to plot points with different marker sizes, and colors under a single handle, while you'd need a loop and get a handle for each point using plot3, which is not only costly in terms of speed, but also in terms of memory. However, if you need to interact with different points (or groups of points) individually - maybe you want to add a separate legend entry for each, maybe you want to be able to turn them on and off separately etc - using plot3 in a loop may be the best (though slow) solution.
For a faster approach, consider this third option (directly uses the low-level function LINE):
line([X,X], [Y,Y], [Z,Z], 'LineStyle','none', 'Marker','o', 'Color','b')
view(3)
Here are some articles discussing plotting performance issues:
Performance: scatter vs. line
Plot performance
Well, if you wanted control over the color of each point, bulk scatter would be faster, because you'd need to call plot separately.
Also, I'm not sure your timing information is accurate because you haven't called drawnow, so the actual drawing could take place after toc.
In summary:
plot3 is fastest because it draws the same marker at many different locations
scatter3 draws many different markers, since size and color of the marker (are allowed to) vary with each point
calling in a loop is really slow, because argument parsing and so forth have to take place repeatedly, in addition as points are added to the plot the axes have to be recalculated
I'm very new to MATLAB and I was trying to display a real time plot of some calculations. I have an N sized vector and I work with m values at a time (say m = N/4), so I want to plot the first m values and then as soon as the second m values are calculated have them replace the first plot.
My approach was as follows:
for i=1:N,
...
//compute m
...
plot(m);
end;
but it fails to update the plot in every loop and waits for all the loops to finish to plot the data. My question is: Should I use another function instead of plot or could I add some delay in each loop?
I think there must be a way I'm not aware of for updating the plot instead of re-plotting it every time.
As Edric mentioned, you'll definitely want to include a drawnow command after the call to plot to force an update of the graphics. However, there is a much more efficient and smoother method to animate plots that doesn't involve recreating the entire plot each time. You can simply initialize your plot, capture a handle to the plot object, then modify the properties of that object in your loop using the set command. Here's an example:
hLine = plot(nan); % Initialize a plot line (which isn't displayed yet
% because the values are NaN)
for i = 1:N % Loop N times
...
% Compute m here
...
set(hLine, 'YData', m); % Update the y data of the line
drawnow % Force the graphics to update immediately
end
In addition, before your loop and after the call to plot you can set a number of axes properties, like the axes limits, etc., if you want the axes to stay fixed and not change their appearance with each new vector m that is plotted.
You can add a call to DRAWNOW to force the plot to update. See the reference page. Note that DRAWNOW causes the graphics event queue to be flushed, which may cause callbacks etc. to be executed.