How to save multiple images by using saveas command?

Hi, i am using stl file reader (https://www.mathworks.com/matlabcentral/fileexchange/22409-stl-file-reader?s_tid=srchtitle) to read stl model and made some changes to obtain multiple images with differen views, so i want save them all. i tried saveas command by wasnt able to save, so can somebody help me? There is a code
fv = stlread('cameraleft.stl');
%%Render
% The model is rendered with a PATCH graphics object. We also add some dynamic
% lighting, and adjust the material properties to change the specular
% highlighting.
for i=0:5:90
k=1:20;
patch(fv,'FaceColor', [0.8 0.8 1.0], ...
'EdgeColor', 'none', ...
'FaceLighting', 'gouraud', ...
'AmbientStrength', 0.15);
% Add a camera light, and tone down the specular highlighting
camlight('headlight');
material('dull');
% Fix the axes scaling, and set a nice view angle
axis('image');
figure
v1=view([i -180]);
axis off
temp=['',num2str(k),'.jpg'];
saveas(gca,temp);
end

5 件のコメント

Image Analyst
Image Analyst 2018 年 6 月 7 日
You forgot to attach 'cameraleft.stl' in a .zip file, and forgot to show where you called saveas().
Baisseyev Miram
Baisseyev Miram 2018 年 6 月 7 日
Thanks, edited
Image Analyst
Image Analyst 2018 年 6 月 7 日
What exactly does "wasnt able to save" mean? Was a jpg file saved or not? If it was saved, does it look OK or not? If it wasn't saved, was an error thrown?
Baisseyev Miram
Baisseyev Miram 2018 年 6 月 7 日
wasnt able to save multiple images, it only saved last one and give name 1 2 3 4...20.jpg
Image Analyst
Image Analyst 2018 年 6 月 7 日
That's because you used k instead of i. You should have done
fileName = sprintf('%d.jpg', i); % Create filename.
saveas(gca, fileName);
See KSSV's code below.

サインインしてコメントする。

回答 (2 件)

Image Analyst
Image Analyst 2018 年 6 月 7 日

2 投票

You used k instead of i. You should have done
fileName = sprintf('%d.PNG', i); % Create filename from i.
saveas(gca, fileName);
I don't even know what the point of k is. Why did you even create it?
Don't use JPG format - use PNG for higher quality. With the code you'll get 0.PNG, 5.PNG, 10.PNG, ..... 90.PNG.

4 件のコメント

Baisseyev Miram
Baisseyev Miram 2018 年 6 月 7 日
thanks it worked but all the saved images a white, i mean there are no object just white background.
Baisseyev Miram
Baisseyev Miram 2018 年 6 月 7 日
also i created k because wanted to be 1.png, 2. png ...
Image Analyst
Image Analyst 2018 年 6 月 7 日
編集済み: Image Analyst 2018 年 6 月 7 日
If you want to save images, try using getframe() and then saving that with imwrite(). Set an image counter to zero before the loop:
imageCounter = 0;
Then, in the loop, get the image and save it with the right name:
rgbImage = getframe(gca);
imageCounter = imageCounter + 1;
fileName = sprintf('%d.PNG', imageCounter); % Create filename from imageCounter.
imwrite(rgbImage, fileName);
Baisseyev Miram
Baisseyev Miram 2018 年 6 月 7 日
Again same problem, firstly rgbImage was struct data type so i made last row like this imwrite(rgbImage.cdata, fileName); and again in saved image was only background i mean without object, when i tried manually(from figure save command) save evething was ok.

サインインしてコメントする。

KSSV
KSSV 2018 年 6 月 7 日

1 投票

Check the below demo code:
N = 10 ;
for i = 1:N
plot(rand(N,1)) ;
saveas(gcf,[num2str(i),'.jpg'])
end

2 件のコメント

Arika Amasarao
Arika Amasarao 2019 年 10 月 18 日
Excellent Sir,
Sir,If i want to save this in new folder?
Image Analyst
Image Analyst 2019 年 10 月 18 日
You'd do
folder = 'c:\whatever'; % Change this to whatever you want.
for k = 1 : N
baseFileName = sprintf('%d.png', k);
fullFileName = fullfile(folder, baseFileName);
saveas(gca, fullFileName); % Save current axes (not the whole figure).
end

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

タグ

質問済み:

2018 年 6 月 7 日

コメント済み:

2019 年 10 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by