How do I create a filename for TIFF's that change in every loop. Like P01.TIFF then P12.TIFF and so forth. I've set up this in strings but I think the single quotes are missing.

3 次查看(过去 30 天)
I have 50 TIFF files that I need to read and operate on. The file names are like P01.TIFF, P12.TIFF, P23.TIFF, P34.TIFF and so forth all the way to P4950.TIFF. I want to open them in a loop. So I need to create a string that suffices for a file name. So I have
for image_no = 1:numi % Loop over number of images
L1 = image_no - 1; % Increment the z depth of the layer's front side
L2 = L1 + 1; % Increment the z depth of the layer's rear side
head = 'P'; % Header for each image file name
num1 = num2str(L1); % Convert L1 to string num1
num2 = num2str(L2); % Convert L2 to string num2
ext = '.tiff';
path = 'S:\LAO\Share\Smalley\FXR\Phase2\PSF\';
name = strcat(path,head,num1,num2,ext); % Create title with strings
PSF = imread(name);
I get the following error message
Error using imread>parse_inputs (line 450)
The file name or URL argument must be a character vector.
Error in imread (line 322)
[filename, fmt_s, extraArgs, was_cached_fmt_used] = parse_inputs(cached_fmt, varargin{:});
Error in Displaytiff (line 32)
PSF = imread(name);
Can anyone help? L1 and L2 each increment by 1 for each cycle through the loop. num1 and num2 are '0' and '1' for the first loop. name is "S:\LAO\Share\Smalley\FXR\Phase2\PSF\P01.tiff" but I think I'm missing single quotes or something. Thanks for your time. Paul
  1 个评论
Stephen23
Stephen23 2018-10-9
编辑:Stephen23 2018-10-9
Do NOT name any variable path, as this shadows the inbuilt path function, which could cause bugs.
Also note that the file extension, the file path, and head remain exactly the same on every loop iteration, so you rather than redefining them 50 times you can just move them outside the loop.

请先登录,再进行评论。

回答(2 个)

Tony Mohan Varghese
Before calling imread, use the following:
name = char(name); % assuming that name is a string array. This will convert to a character vector.

Stephen23
Stephen23 2018-10-9
编辑:Stephen23 2018-10-9
Your code is very verbose for such a simple operation. I recommend using neater sprintf to do the job:
D = 'S:\LAO\Share\Smalley\FXR\Phase2\PSF\';
for k = 1:numi
F = sprintf('P%d%d.tiff',k-1,k);
I = imread(fullfile(D,F));
...
end

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by