How can I load multiple tiff files through a loop?

I am trying to load multiple tiff files like this:
for i=66:96
LSTday=strcat('Research\MODIS\MOD11A1\MOD11A1.006_LST_Day_1km_doy20101',string(i));
end
but, I keep getting imread error: Error using imread>parse_inputs (line 450)
The file name or URL argument must be a character vector.
Can anyone kindly help me what is the issue?

2 Kommentare

Which MATLAB release are you using? There were a small number of releases after string() was introduced but before imread() recognized it.
Mery960
Mery960 am 13 Mär. 2022
I am using 2018a version

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Image Analyst
Image Analyst am 13 Mär. 2022
See code samples in the FAQ:
Here is code I adapted from it for you:
folder = 'Research\MODIS\MOD11A1\MOD11A1.006_LST_Day_1km_doy20101';
if ~isfolder(folder)
errorMessage = sprintf('Folder %s does not exist.\n', folder);
uiwait(errordlg(errorMessage));
return;
end
% Use fullfile() if you need to prepend some other folder to the base file name.
% Adapt to use which ever cases you need.
for k = 66 : 96
% Create an image filename, and read it in to a variable called imageData.
fullFileName = fullfile(folder, sprintf('%d.tiff', k));
if isfile(fullFileName)
imageData = imread(fullFileName);
imshow(imageData, []);
drawnow;
else
fprintf('File %s does not exist.\n', fullFileName);
end
end

5 Kommentare

Mery960
Mery960 am 14 Mär. 2022
Thank you. But I got errors as I have a string(i) with each image and that is not read in the loop.
Stephen23
Stephen23 am 14 Mär. 2022
Bearbeitet: Stephen23 am 15 Mär. 2022
"But I got errors as I have a string(i)..."
Your code has errors. Image Analyst's code does not call STRING anywhere.
So far Walter Roberson explained that you cannot use a string with IMREAD. Yet you persist in trying to use a string input to IMREAD. What do you expect to change if you keep doing the same thing?
Mery960
Mery960 am 14 Mär. 2022
I understood that but it is not looping through the image as the last part is changing from 66:96 as in this part LSTday=strcat('Research\MODIS\MOD11A1\MOD11A1.006_LST_Day_1km_doy20101',string(i));
What can I put there instead of a string? the code image analyst shared gives this error:
Researcj\MODIS\MOD11A1\MOD11A1.006_LST_Day_1km_doy20101\96.tiff does not exist. (whereas the data isn't set like this.
Stephen23
Stephen23 am 14 Mär. 2022
Bearbeitet: Stephen23 am 15 Mär. 2022
"What can I put there instead of a string?"
Did you try the suggestions that you have been given so far on this thread?
  • CHAR of a string scalar
  • SPRINTF of a numeric
Or use DIR, which probably suits your use-case better anyway. See:
Actually you should probably use the other method in the FAQ where you just get a list of everything that's definitely there, rather than build up a filename and hope that it's there. But anyway, either way will work, IF you'll just follow the FAQ instead of continuing to try to use your way.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Walter Roberson
Walter Roberson am 13 Mär. 2022

0 Stimmen

In R2018a, imread() requires a character vector, not a string() object.
The first release that supported string objects for file names was R2018b, the release after you have.

3 Kommentare

Mery960
Mery960 am 13 Mär. 2022
Is there any other function I can use to read the string file in a loop then? I have way too many data to load.
Stephen23
Stephen23 am 13 Mär. 2022
Bearbeitet: Stephen23 am 13 Mär. 2022
"Is there any other function I can use to read the string file in a loop then?"
Use a character vector rather than a string, just as Walter Roberson wrote.
"I have way too many data to load."
The MATLAB documentation show how to load multiple files. Note that it recommends SPRINTF:
char() of a string scalar gives you a character vector

Melden Sie sich an, um zu kommentieren.

Voss
Voss am 14 Mär. 2022
In place of the code you posted, do this:
file_name_prefix = 'Research\MODIS\MOD11A1\MOD11A1.006_LST_Day_1km_doy20101';
for i = 66:96
LSTday = sprintf('%s%d',file_name_prefix,i);
end
And use LSTday the same as you are already doing in the rest of the code, e.g., using it in imread().

2 Kommentare

Except that I think you really meant to use fullfile() which is more robust since it doesn't care if the folder ends with a slash or not - it will always get it right.
Voss
Voss am 15 Mär. 2022
Well, I didn't want to assume that 'MOD11A1.006_LST_Day_1km_doy20101' is a directory when it might be part of the file names, e.g., file names might be:
  • MOD11A1.006_LST_Day_1km_doy2010166
  • MOD11A1.006_LST_Day_1km_doy2010167
  • etc.
OP has reported: "Researcj\MODIS\MOD11A1\MOD11A1.006_LST_Day_1km_doy20101\96.tiff does not exist." but that could be for any one of a number of reasons.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Convert Image Type finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2018a

Gefragt:

am 13 Mär. 2022

Kommentiert:

am 15 Mär. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by