How to saveas multiple figure in folder using matlab ?

1 回表示 (過去 30 日間)
Armylia Dewi
Armylia Dewi 2022 年 6 月 17 日
回答済み: Karim 2022 年 6 月 17 日
I want to save the figure in .jpeg format in a new folder. i get an error in this code. please help me to solve this problem. I would be very grateful.
image_folder ='E:\Resize 200'
filenames = dir (fullfile(image_folder,'*.jpeg'))
total_images = numel(filenames);
for n= 1:total_images;
f= fullfile(image_folder, filenames(n).name);
ambang = 40; % ambang mulai dianggap bagian badan
citra = imread(f);
[tinggi, lebar] = size(citra); % ukuran citra
%figure; imshow(citra);
baris_tengah = citra(floor(tinggi / 2), :); % baris tengah citra
kiri = 1; % nilai awal batas kiri
kanan = lebar; % nilai awal batas kanan
for i = 25:lebar % cari dari kiri
if baris_tengah(i) > ambang % kalau sudah tercapai,
kiri = i; % simpan letaknya
break; % berhenti cari
end;
end;
for i = (lebar - 25):-1:1 % cari dari kanan
if baris_tengah(i) > ambang % ,
kanan = i; %
break; %
end;
end;
%figure; hold on;
%plot(baris_tengah);
%plot([kiri kiri], [0 255], 'r'); % batas kiri
%plot([kanan kanan], [0 255], 'r'); % batas kanan
%hold off;
%hasil pangkas
pangkas = imcrop(citra, [kiri 0 (kanan - kiri) tinggi]);
figure;
imshow(pangkas);
saveas (figure, 'baru',f(i));
end

回答 (1 件)

Karim
Karim 2022 年 6 月 17 日
Hello,
Witouth the figures or the error message it's not really clear which error you obtain. But at first glance the saveas command doesn't seem correct. The variable f holds the full filename of the original picture, and you ask the i'th element from that.
Can you try to change the command into something like:
currFig = figure;
imshow(pangkas);
saveas(currFig, fullfile(image_folder, ['baru ' filenames(n).name]));
By doing so you basically append the original filename with "baru". Note that this will save it in the orginal folder, but with a different name.

カテゴリ

Help Center および File ExchangePrinting and Saving についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by