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
Tony Mohan Varghese 2018년 3월 21일
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