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 个评论

The line
display(strcat(R, '.png'));
provides me the string I need.
But I cannot make it usable inside the imwrite line...
In this way it takes the value of R but not the extension png.
imwrite(X.cdata, R{l}, 'png')
I'm working this inside a loop. When it loops it breaks and gives me a message:
??? Index exceeds matrix dimensions.
... this way it takes the complete filename the first time but breaks when it loops:
imwrite(X.cdata, [R{l}, '.png'])
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 个)

R='MyImageFile';
imwrite(X.cdata,[R,'.png']);

6 个评论

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.
Thank you Fangjun,
However, it saves a file named MyImageFile.png, not a file named with the value of R.
I forgot to mention that R is a cell in a cell array (it's text).
Thank you Image Analyst,
I'm going to take a look at the pages you point me to.
Best,
Diego
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.
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']);
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
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}

请先登录,再进行评论。

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 个评论

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
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

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by