Continuous stretch in MATLAB

I have a loop in my code and I want to build some variables,

At each iteration, I draw a new point, and I want it to be connected to the previous point.

Here is an example code (in this code, a loop is not needed, but in the actual code this is not so).

n = 500; Fs = 1000; f1 = 10; t = 0; dt = 1 / Fs; for i = 1 : n s = sin(2 * pi * f1 * t); t = t + dt; plot(t,s,'bo'); hold on; axis([0 t(end) -1 1]); end 

enter image description here

Right is what I want.

I was thinking about using line , but that would be messy (I would have to change i and use 4 in each line command).

It seems to me that this is a simple question, maybe I missed something.

Thanks for any help.

+2
source share
3 answers

This is similar to @Nras answer, but faster and easier to read. I have several programs that do such things, and depending on how long your computing cycle is, updating your graphics can be a significant and annoying bottleneck.

If you know how long your vector will eventually end up, you can pre-select your plot and then update using the handle command:

 n = 500; Fs = 1000; f1 = 10; t = 0; dt = 1 / Fs; s = nan(1,n); emptyvec = nan(1,n); h1 = plot(emptyvec,emptyvec,'-bo'); h2 = handle(line(emptyvec,emptyvec,'Color','r','Marker','x','LineStyle','--')); h1 = handle(h1); for i = 1 : n t = t + dt; s = sin(2 * pi * f1 * t); h1.XData(i) = t; h1.YData(i) = s; h2.XData(i) = t; h2.YData(i) = s^2; drawnow end 

MATLAB ignores nan when calculating limits, so pre-allocating with nan makes xlim unnecessary. Also, pause not required when I run this. Your mileage may vary. I personally prefer to do something like this:

 h = plot(t,emptyvec); 

As long as t is famous and not crazy, it gives me an idea not only of how the calculation is performed, but also gives me a kind of progress bar. If it's crazy, I can fill t in chunks or use XData and YData as a loop buffer using ii = mod(i,100); as an index (for example), which will lead to an oscilloscope type effect. In addition, there is a time limit for constantly recalculating the axis limits.

If you don’t know how long your vector will be (if it is in a while , for example), you can either pre-pass the vector, which, as you know, will be longer, select it in pieces, or simply make a circular buffer object.

For multiple charts, preallocate uses the line function for each additional line. Unlike plot , you can simply transfer the more primitive line function to the handle function.

Please note: if you use MATLAB 2014b or more (I don’t), the new HG2 graphics system uses objects for descriptors rather than doubles, so the handle command, which converts a numerical descriptor to a handle object, is redundant, and the point designation can be used directly . Also note that handle used in this way undocumented

+4
source

You can use the graph handle and then update the "XData" and "YData" properties, respectively. Therefore, you can build the first point before the loop and form a graph handle. After that, the graph descriptor is available, and the graph can be adjusted within the cycle.

 n = 500; Fs = 1000; f1 = 10; t = (0 : n - 1) / Fs; s = zeros(size(t)); s(1) = sin(2 * pi * f1 * t(1)); figure handle = plot(s(1), t(1), 'bo-'); %// the plot handle for i = 2 : n s(i) = sin(2 * pi * f1 * t(i)); set(handle, 'XData', t(1:i), 'YData', s(1:i)) %// update the plot data axis([0 t(end) -1 1]); pause(0.1) %// small pause to see animation end 
+4
source

I would replace your existing plot string with this

 plot(t(1:i),s(1:i)); hold on; 

This will display all points from index 1 to the current index. Then, removing 'bo' , it will build the default format, which is the desired string.

Finally, if you want it to actually be animated on the screen (for example, in a movie), you need to add the drawnow command to the end of your loop. You can also add pause(0.25) after the draw to insert a quarter second delay so that your eye has the opportunity to see the newly drawn image before it is overwritten with the next image.

+1
source

Source: https://habr.com/ru/post/1208694/


All Articles