0

I am attempting to convert a MATLAB code to Python, and there are 2 lines in specific that are giving me issues.

h = findobj('Type', 'Figure');
cur_fig = get(0, 'CurrentFigure');

How do I convert this to python? I tried using plt.figure() and plt.gcf(), but later in the script, I need h to be a list to use the set function in MATLAB.

To give a better sense of what I am doing, the total MATLAB code is below.

h = findobj('Type','figure');
cur_fig = get(0,'CurrentFigure');
warning off
for i = 1:length(h)
    set(0,'CurrentFigure',h(i));
    cur_ax = get(h(i),'CurrentAxes'); % gets current axis handle for given             figure
    hidn_axis=axes('Position',[0 0 1 1], 'Visible','off','Units','normalized','Tag','Stampit');
    dh(i,1) = text(.905,0.01,plotID);
    set(dh(i,1),'FontSize',[12],'HorizontalAlignment','Right');

    set(h(i),'CurrentAxes',cur_ax); % Sets current axis back to its previous state

    end                                          
end

set(0,'CurrentFigure',cur_fig);
pause(.001)
warning on

return;

The code necessary is just the following:

h = findobj('Type','figure');
cur_fig = get(0,'CurrentFigure');
for i = 1:length(h)
    set(0,'CurrentFigure',h(i));
    cur_ax = get(h(i),'CurrentAxes'); % gets current axis handle for given figure

I tried to do the following:

h = plt.figure()
cur_fig = plt.gcf()
for i = range(1,len(h)+1):
    plt.set(0,'CurrentFigure',h[i]);

However, I got an error message that said

TypeError: object of type 'Figure' has no len()

Adriaan
  • 17,741
  • 7
  • 42
  • 75
elarr
  • 341
  • 1
  • 2
  • 17

1 Answers1

0

For anyone wondering, the best way I found to do this was using enumerate, as shown in the following:

for i, figure in enumerate(figures):
    figure.savefig('figure%d.png' % i)
elarr
  • 341
  • 1
  • 2
  • 17