How can you control a variable during a Matlab loop without printing it?

I run a loop in which variables are computed. It would be useful to see the current values ​​of these variables. It is not practical to print them, because other parts of the loop print a lot of text. In addition, the Workspacevalues ​​are not displayed on the tab until the end of the cycle.

Is there a way to control these fi variables by printing them to a window?

+4
source share
1 answer

You can create a shape with an object textand update its property 'string'according to the required variable:

h = text(.5, .5, ''); %// create text object
for n = 1:1000
    v = n^2; %// loop computations here. Variable `v` is to be displayed
    set(h, 'string', ['Value: ' num2str(v)]);
    drawnow %// you may need this for immediate updating
end

For greater speed, you can only update every Siteration:

h = text(.5, .5, ''); %// create text object
S = 10; %// update period
for n = 1:1000
    v = n^2; %// loop computations here. Variable `v` is to be displayed
    if ~mod(n,S) %// update only at iterations S, 2*S, 3*S, ...
        set(h, 'string', ['Value: ' num2str(v)]);
        drawnow %// you may need this for immediate updating
    end
end

drawnow('limitrate'), @Edric:

h = text(.5, .5, ''); %// create text object
for n = 1:1000
    v = n^2; %// loop computations here. Variable `v` is to be displayed
    set(h, 'string', ['Value: ' num2str(v)]);
    drawnow('limitrate')
end
+6

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


All Articles