imwrite with variable filename

조회 수: 2 (최근 30일)
Nicolas
Nicolas 2013년 10월 30일
댓글: Walter Roberson 2016년 4월 9일
Hi,
I'm trying to call imwrite in a loop:
ori_dir = 'path/to/folder'
files = {'fi','le','na','me'};
for k = 1:length(files)
% do some operation on img %
write_dir = strcat(ori_dir, '/', files(k),'.tif');
imwrite(img, write_dir);
end
I'm getting the following error
Error using imwrite>parse_inputs (line 510)
A filename must be supplied.
Error in imwrite (line 418)
[data, map, filename, format, paramPairs] = parse_inputs(varargin{:});
write_dir contains a string to the correct path however. If I paste the string directly in imwrite, I don't have any issue, eg:
imwrite( img, 'path/to/folder/fi.tif')
does not raise an error.
I'm not quite sure what is wrong here. The variable files is a cell array with only strings in it.
Any help would be really appreciated!

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 10월 30일
Correct this line
write_dir = strcat(ori_dir, '/', files{k},'.tif')
  댓글 수: 3
Walter Roberson
Walter Roberson 2016년 4월 9일
bill jones comments
doesnt actually provide an answer.
Walter Roberson
Walter Roberson 2016년 4월 9일
bill jones:
The original line was
write_dir = strcat(ori_dir, '/', files(k),'.tif');
the new suggested line is
write_dir = strcat(ori_dir, '/', files{k},'.tif');
This is different. Notice that files(k) has been changed to files{k} . files is a cell array of strings, so files(k) is a 1 x 1 cell array that contains a string. When you strcat() something that contains a cell array of strings, the result is a cell array of strings, but imwrite() cannot handle a file name which is a cell array of strings. files{k} on the other hand is the string extracted from the cell array. That leaves you strcat() only strings, which gives a string result, and imwrite() can deal with that.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Color and Styling에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by