imwrite filename
이전 댓글 표시
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
2011년 12월 29일
Diego
2011년 12월 29일
Diego
2011년 12월 29일
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
채택된 답변
추가 답변 (3개)
Fangjun Jiang
2011년 12월 29일
R='MyImageFile';
imwrite(X.cdata,[R,'.png']);
댓글 수: 6
Image Analyst
2011년 12월 29일
I'd also recommend the use of fullfile() to make certain where you're going to save it rather than just saving it wherever the current folder happens to be. Since (because you're saving cdata) it appears you're saving more than just an image, but an image with perhaps some graphics in the overlay, you might be interested in Oliver's export_fig in the File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A29192 It's the most downloaded file.
Diego
2011년 12월 29일
Image Analyst
2011년 12월 29일
Diego, see my code below that works if your R is a cell. Basically you need to use cell2mat() to turn it into a character array.
Fangjun Jiang
2011년 12월 29일
If R is a cell array, just use R{i} to refer to the text.
R={'a','b','c'};
imwrite(X.cdata,[R{i},'.png']);
Diego
2011년 12월 29일
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
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
2011년 12월 29일
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
2012년 7월 1일
0 개 추천
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
카테고리
도움말 센터 및 File Exchange에서 Image Arithmetic에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!