MATLAB Answers

How to load selected files, in a sequence, and implement them in a for loop?

8 views (last 30 days)
Lina Koronfel
Lina Koronfel on 30 Dec 2019
Commented: Lina Koronfel on 21 Feb 2020 at 4:30
Hi,
I'm writing a code that process multiple images in a sequential order from a folder (basically frames of some video). However, my problem is that I want to collect information and process only specific frames that repeat after a constant frame number ( I want to process 400 frames, every 800 frames, and keep doing this for the length of the files wich is X0000 frames). I don't know which part of the code I should write that command though. Below is the first part of the code until loading the files. VIPFrames is my idea for which frames I want to call. Thank you!
myFolder= cd;
if exist(myFolder, 'dir')~= 7
Message = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(Message));
return;
end
tifFilesNotOrg=dir(fullfile(myFolder,'*.tif'));
tifFiles=natsortfiles({tifFilesNotOrg.name});
for F=1:length(tifFiles);
VIPFrames=200+(800.*F-800):600+(800.*F-800); %Limited Frames-stimulation-how to place it?
fullFileName=fullfile(myFolder,tifFiles{F});
fprintf('Now reading %s\n', fullFileName);
imageArray=imread(fullFileName,VIPFrames);
end

  0 Comments

Sign in to comment.

Accepted Answer

dpb
dpb on 30 Dec 2019
Edited: dpb on 30 Dec 2019
"I want to process 400 frames, every 800 frames, and keep doing this for the length of the files..."
...
nProc=400;
nSkip=800;
tifFiles=dir(fullfile(myFolder,'*.tif'));
tifFiles=natsortfiles({tifFiles.name});
for i=1:nSkip:numel(tifFiles)
for j=i:i+nSkip-1
fullFileName=fullfile(tiffFiles(j).Folder,tifFiles(j).Name);
fprintf('Now reading %s\n', fullFileName);
imageArray=imread(fullFileName);
% whatever else here
end
end

  4 Comments

Show 1 older comment
Stephen Cobeldick
Stephen Cobeldick on 2 Jan 2020
"There is a problem..."
Here are two solutions:
Method one: DIR output structure: Due to this line of code:
tifFiles=natsortfiles({tifFiles.name});
the structure tifFiles is replaced by a cell array of char vectors with the same name, so later when dpb tries to access the structure it throws an error. You can instead sort the dir output structure by using natsortfiles' second output, the sort index:
[~,idx] = natsortfiles({tifFiles.name});
tifFiles = tifFiles(idx);
How to sort the dir output structure is also shown in the natsortfiles "Examples" documentation, as this is such a common action. This is why it is recommended to read the documentation of every function that you use.
Method two: cell array of character vectors: A simpler alternative is to just use that cell array, something like this:
S = dir(fullfile(myFolder,'*.tif'));
C = natsortfiles({tifFiles.name});
for ...
for ...
F = fullfile(myFolder,C{j});
...
end
end
Method two is what you used in your original question. Unless dpb offers a good reason for sorting the dir output structure, feel free to continue to use Method two in your code.
dpb
dpb on 2 Jan 2020
"Unless dpb offers a good reason for sorting the dir output structure, ..."
Nothing more than consistency in way I tend to write code can I offer, no.
I just didn't think about the fact had turned the struct into a cell array as I was writing the response...I used my natural reference to the dir() struct by pure rote.
I am big on keeping the same style everywhere, however, so if it were my code needing to do this, I'd guess I'd sort the dir() struct about 99.73% of the times it came up (which would be almost never in the example as unless the files came from an outside source, I'd have named them such as they would sort alphanumerically and thereby already sorted as returned by dir). Unless Stephen has a good reason to NOT sort the dir() struct, ... <VBG>
However, it's your code, use the way that seems most natural to you. I tend to avoid cell arrays when I can; I'm always tending to forget the curlies and so make more coding errors to have to fix when do...
Lina Koronfel
Lina Koronfel on 21 Feb 2020 at 4:30
Thank you alot for the help! I implemented your answers and now I used the code multiple times and it is working fine. This is the final code I'm using in case it would benefit others. Cheers.
ImpFrames=250; %Truncate trial to only important frames
StartInt=800;
tifFiles=dir(fullfile(myFolder,'*.tif'));
tifFiles=natsortfiles({tifFiles.name});
for i=200:StartInt:8000;%length(tifFiles)----All frames of trials to be analyzed;
for j=i:i+ImpFrames; %Selected frames/trial to be analyzed
fullFileName=fullfile(myFolder, tifFiles{j});
fprintf('Now reading %s\n', fullFileName);
imageArray_uncrop=imread(fullFileName);
%whatever...
end
end

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Tags


Translated by