Problems in order to concatenate a string with file extension at filename parameter of imwrite

Hi everyone,
i'm trying to save some images consecutively changing the filename of each one from a matrix which contains the parameters.
That is, the filenames would be: 'name_3_P1' 'name_4_P1' 'name_3_P2' 'name_4_P2'
where each parameter separated by '_' it's a position in that matrix.
Once I have obtained the string in a variable with the desired name, imwrite don't accept this string, because it forces me to add an extension. So, I'm looking for an alternativa for concatenating the string with the extension at the filename.
Any solutions? Thank you.

1 Kommentar

@Emilio: some comments:
  • the imwrite documentation includes a list of supported image file formats, but .AVI is not listed.
  • MATLAB does not distinguish between supplying the string directly as an argument or as a variable.
  • Because the image file format is determined from the file extension, using a file name like 'name.avi_1_P1' will not work, as this is not a known file extension. This means your comment to Thorsten's answer "For example, it's not important if the string is 'name_3_P1' or 'name.avi_3_P1'." is incorrect.
There is nothing wrong with imwrite, it works perfectly following the syntax given in the documentation:
>> load spine
>> name = sprintf('temp_%d.png',3)
name =
temp_3.png
>> imwrite(X,map,name)
And here is the new image file shown in the Current Folder:

Melden Sie sich an, um zu kommentieren.

Antworten (2)

You can use name_3_P1.avi

2 Kommentare

I'm afraid that you don't understand my answer, or I coudn't be able to explain properly.
I mean, the problem it's not the name or the format of the string, but the call of imwrite. For example, it's not important if the string is 'name_3_P1' or 'name.avi_3_P1'.
I'm trying to call imwrite(image,filename), where filename has to be a string, but concatenated with an extension, as 'myimage.png'. The problem would be if I'm trying to put a string variable instead a string directly ( imwrite(image,name) instead imwrite(image,'name_3_P1.png') ), because Matlab send me an error related the lack of an extension, whatever if I have concatenated the extension previously (namestring = 'name_3_P1.png') or I'm trying this call "imwrite(image,strcat(name,'.png').
Can you run the example that Jan posted? I tried the same, and it works smoothly.

Melden Sie sich an, um zu kommentieren.

There must be another problem. Please post the relevant part of the original code and the complete error message. Currently we have to guess too many details.
At least this works:
FileName = 'name_3_P1.png'
imwrite(rand(100, 100, 3), FileName)

Kategorien

Mehr zu Convert Image Type finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 27 Mai 2015

Kommentiert:

am 28 Mai 2015

Community Treasure Hunt

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

Start Hunting!

Translated by