I made a folder with 12 .fig files, I am attempting to convert these to jpeg using a loop.
11 ビュー (過去 30 日間)
古いコメントを表示
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. 

0 件のコメント
採用された回答
DGM
2024 年 4 月 4 日
編集済み: DGM
2024 年 4 月 4 日
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 件のコメント
DGM
2024 年 4 月 4 日
oof I can't believe I forgot that. That's what I get for testing with only one file.
その他の回答 (1 件)
Mitchell Thurston
2024 年 4 月 4 日
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で File Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!