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.

2 views (last 30 days)
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 Comment
Stephen23
Stephen23 on 9 Oct 2018
Edited: Stephen23 on 9 Oct 2018
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.

Sign in to comment.

Answers (2)

Tony Mohan Varghese
Tony Mohan Varghese on 21 Mar 2018
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 on 9 Oct 2018
Edited: Stephen23 on 9 Oct 2018
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

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!