Plot index of for loop

5 次查看(过去 30 天)
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!

采纳的回答

chicken vector
chicken vector 2023-4-22
编辑:chicken vector 2023-4-22
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by