How to load selected files, in a sequence, and implement them in a for loop?
1 次查看(过去 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 个评论
采纳的回答
dpb
2019-12-30
编辑:dpb
2019-12-30
"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 个评论
dpb
2020-1-2
编辑:dpb
2020-1-4
"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...
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!