Clear Filters
Clear Filters

Plot index of for loop

5 views (last 30 days)
kale
kale on 22 Apr 2023
Edited: chicken vector on 22 Apr 2023
I have a for loop that plots a graph that dynamically updates (don't look at the what the code does and what it plots, it doesn't matter).
I would like to print simultaneously with the dynamic plot (generated with "plot" and "drawnow") the index "i" of the loop, to see what index that given iteration/plot corresponds to.
I think the sprintf function should be used, but anyway if another function should be used tell me. I did some tests with sprintf but could not produce the result I hoped for
I am attaching the code, if you want to update it or tell me how I can solve this problem .. thank you in advance!

Accepted Answer

chicken vector
chicken vector on 22 Apr 2023
Edited: chicken vector on 22 Apr 2023
I used annotation because accepts normalised units, otherwise text might be a better option. Have a look at it.
Also I stronlgy suggest you to avoid naming variables with function names, such as length.
Finally, unless needed for demonstration purposes, avoid live scripts and use normal scripts.
By doing so you can initialise a figure outside the loop, instead of setting the same figure title at each iteration with title('dynamic graph'); inside the loop.
for i=1:length %ciclo for 200 giri
in=circshift(in,1);
in(1)=seq(i);
ff = sum(in.*variable_f);
if i>n
e = ff - seq(i-n);
variable_f= variable_f - step*e*conj(in);
end
plot(variable_f)
% Command to show current iteration (add this to your loop):
iteration = annotation('textbox', [.7 .7 .1 .1], ...
'EdgeColor', 'None', ...
'String', [num2str(i) '/' num2str(length)], ...
'FontSize',20);
drawnow
title('dynamic graph');
end

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!