imwrite filename

5 ビュー (過去 30 日間)
Diego
Diego 2011 年 12 月 29 日
Dear all, How can I save an image with the file name being the value of a variable?
The following code works in exporting the image but not in assigning the file name the way I need.
R is the name of the variable...
Thanks, Diego
Matlab code
imwrite(X.cdata, 'R''.png')
  4 件のコメント
Diego
Diego 2011 年 12 月 29 日
... this way it takes the complete filename the first time but breaks when it loops:
imwrite(X.cdata, [R{l}, '.png'])
Muhammad Ghani
Muhammad Ghani 2012 年 7 月 1 日
Let's say you are working in the loop, use the following for k=1:30 imwrite(filename, sprintf('name of the image to be stored_%d.png',k) ); end Hope that it helps. Ghani

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

採用された回答

Diego
Diego 2011 年 12 月 29 日
Thank you Fangjun and Image Analyst. Your tip Fangjun point me to the answer.
imwrite(X.cdata, [R{1}, '.png'])
Regards,
Diego

その他の回答 (3 件)

Fangjun Jiang
Fangjun Jiang 2011 年 12 月 29 日
R='MyImageFile';
imwrite(X.cdata,[R,'.png']);
  6 件のコメント
Diego
Diego 2011 年 12 月 29 日
Thank you Fangjun.
Yes i did that already, and it works the first time it loops, but then it breaks with a message ??? Index exceeds matrix dimensions.
Best,
Diego
Fangjun Jiang
Fangjun Jiang 2011 年 12 月 29 日
That means you did more loops than the number of cells you have in R. Do the following to see the exact error message.
clear R;
R={'a','b','c'};
R{4}

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


Image Analyst
Image Analyst 2011 年 12 月 29 日
R = {'MyFileName.png'};
% Construct filename.
myFolder = 'D:\myImageFiles';
if ~exist(myFolder)
% mkdir(myFolder);
end
% Create character version of the cell.
charR = cell2mat(R);
% Create the full filename.
fullFileName = fullfile(myFolder, charR)
% Write out the image file to disk.
imwrite(imageArray, fullFileName);
  2 件のコメント
Diego
Diego 2011 年 12 月 29 日
Thank Image Analyst.
But this way it creates a file named 'MyFileName.png' and overwrites it over and over again.
Probably I'm doing something wrong...
Best,
Diego
Image Analyst
Image Analyst 2011 年 12 月 29 日
Yes. What do you mean over and over again? You never said anything about this being in a loop where you need to change the filename inside the loop. If you do, use sprintf() to construct the filename. Or see the FAQ http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F if you got the filenames into a cell array from the dir() function.

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


Muhammad Ghani
Muhammad Ghani 2012 年 7 月 1 日
Let's say you are working in the loop, use the following
for k=1:30 imwrite(filename, sprintf('name of the image to be stored_%d.png',k) ); end
Hope that it helps.
Ghani

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by