imwrite with variable filename
4 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Azzi Abdelmalek
2013-10-30
Correct this line
write_dir = strcat(ori_dir, '/', files{k},'.tif')
3 个评论
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 Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!