I made a folder with 12 .fig files, I am attempting to convert these to jpeg using a loop.

28 vues (au cours des 30 derniers jours)
Jackson
Jackson le 4 Avr 2024 à 18:54
Commenté : DGM le 4 Avr 2024 à 22:13
I have tried multiple loops and commands but I am new to matlab and have still not been able to figure it out.
figs = openfig('Figures');
for K = 1 : length(figs)
filename = sprintf('figure_%02d.jpg', K);
saveas(figs(K), filename);
end
This is what I have.
Error using load
Unable to read file 'Figures'. Input cannot be a directory.
Error in matlab.graphics.internal.figfile.FigFile/read (line 31)
hgDataVars = load(filename, '-mat', '-regexp', '^hg[M]');
Error in matlab.graphics.internal.figfile.FigFile
Error in loadFigure (line 31)
FF = matlab.graphics.internal.figfile.FigFile(fullpath);
Error in openfig>localOpenFigure (line 75)
h = loadFigure(filename, visibleAction);
Error in openfig (line 40)
figOut = localOpenFigure(filename, reuse, visibleAction);
Error in P2B (line 1)
figs = openfig('Figures');
These are the errors I am getting and the pictures are the figures the the folder.

Réponse acceptée

DGM
DGM le 4 Avr 2024 à 19:10
Modifié(e) : DGM le 4 Avr 2024 à 19:11
Maybe something more like this.
D = dir('Figures/*.fig'); % use dir() to get directory contents
for K = 1:numel(D)
hf = openfig(fullfile(D.folder,D.name)); % get the full name
filename = sprintf('figure_%02d.png', K); % don't use JPG for anything
saveas(hf, filename); % save the opened figure
close(hf) % close the opened figure instead of creating a pile of them
end
... assuming there are less than 100 .fig files.
  5 commentaires
DGM
DGM le 4 Avr 2024 à 22:13
oof I can't believe I forgot that. That's what I get for testing with only one file.

Connectez-vous pour commenter.

Plus de réponses (1)

Mitchell Thurston
Mitchell Thurston le 4 Avr 2024 à 19:12
You can do it by running this code while inside the Figures directory
files = ls; % get filenames of everything in that directory
K = 0;
for i = 1:length(files(:,1))
[~,~,ext] = fileparts(files(i,:));
if strcmp(strip(ext),'.mat')
K = K+1;
fig = openfig(files(i,:));
filename = sprintf('figure_%02d.jpg', K);
saveas(fig, filename);
end
end
Also, if the figures are generated by P2B.m, you should be able to get all the figures outputted by publishing that script.

Catégories

En savoir plus sur Printing and Saving dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by