import over 1000 files
Afficher commentaires plus anciens
i want to change many files including files in subfolders.
but if file outnumber 1001, my code stops.
i'd like to import over 1000 files to "files" struct.


2 commentaires
Rik
le 31 Juil 2021
What code are you using to select the files?
Jeong Dae Kim
le 31 Juil 2021
Réponse acceptée
Plus de réponses (3)
Chunru
le 31 Juil 2021
0 votes
You may hit the limit os the number of files that can be opened simutaneously. While it possible to increase the maximum number of opened files in OS, you may want to try open file one by one and then close the file once it is not used.
Image Analyst
le 31 Juil 2021
Try code in the FAQ: https://matlab.fandom.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
It will work for more than 1000 files, if you have more than that.
% Specify the folder where the files live.
myFolder = 'G:\';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.png'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
numFiles = length(theFiles)
Result = zeros(numFiles, 1);
for k = 1 : numFiles
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf('Now reading %s, file #%d of %d.\n', fullFileName, k, numFiles);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray = imread(fullFileName);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
Result(k) = mean(imageArray(:)); % Whatever you want.
end
14 commentaires
Jeong Dae Kim
le 31 Juil 2021
Image Analyst
le 31 Juil 2021
@Jeong Dae Kim What was wrong with my code? Just put your 3 lines of code at the top of mine (replacing my dir()) and change variable names appropriately and you're good to go.
Jeong Dae Kim
le 31 Juil 2021
Modifié(e) : Jeong Dae Kim
le 31 Juil 2021
Rik
le 31 Juil 2021
No, your code fails. The reason is that you're telling matlab you want a 1x2x3x4x5x...x1000 array, instead of a 1x1000 vector.
@Jeong Dae Kim: you request a huge array, much larger than any normal computer could hold in memory:
Result = zeros(1:numFiles);
This requests an array of size 1x2x3x4x5...xnumFiles. For 1000 files this array will contain exactly 402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 elements (approx. 1e2567). Multiply that by 8 to get the required number of bytes.
Does your computer have that much memory? (hint: the observable universe has around 3.28e80 particles)
Jeong Dae Kim
le 31 Juil 2021
Image Analyst
le 31 Juil 2021
@Jeong Dae Kim, Why did you declare Results twice? And why is the final one a cell array? Do you need a cell array?
Please attach your entire .m file so we can fix it.
Jeong Dae Kim
le 1 Août 2021
Rik
le 1 Août 2021
Well, then you have only 1000 that match your pattern. I don't believe there are releases without the recursive ** for dir that have live scripts.
How do you want to store your results? In a vector? An array? And what data type should it be?
Also, next time you're posting code, please post it as code (not as an image). You could even attach your mlx file.
Jeong Dae Kim
le 1 Août 2021
Rik
le 1 Août 2021
Why would you? The char data type supports full Unicode, so even CKJ characters are fully supported. As far as I tested the string data type also support full Unicode (although the readlines function does seem to have some issues with emoji (or more correctly: characters outside the BMP)).
Why do you want to convert them? To what?
And why don't you answer the questions you're getting?
Image Analyst
le 1 Août 2021
I believe this should work to find all .mp4 and .mpg files in the folder plus subfolders. Make sure to assign the desired starting folder, myFolder. If it doesn't work, attach your m-file.
% Specify the folder where the files live.
myFolder = pwd; %'G:\';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '**/*.mp4') % Change to whatever pattern you need.
pngFiles = dir(filePattern);
filePattern = fullfile(myFolder, '**/*.mpg') % Change to whatever pattern you need.
bmpFiles = dir(filePattern);
theFiles = [pngFiles; bmpFiles];
numFiles = length(theFiles)
Result = zeros(numFiles, 1);
for k = 1 : numFiles
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf('Now reading %s, file #%d of %d.\n', fullFileName, k, numFiles);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
Result(k) = whatever; % Whatever you want.
end
Jeong Dae Kim
le 2 Août 2021
Rik
le 2 Août 2021
Please show all your code, exactly as you try to use it.
michael adams
le 14 Déc 2023
0 votes
I can confirm this is an issue and it is not consistent. If I query many directories (all with more than 1000 files), sometimes 'dir' returns the correct structure of all files, but sometimes it just stops at 1000.
I do not run into this issue on local drives. I see this issue on network drives only.
3 commentaires
Rik
le 15 Déc 2023
How did you confirm this is an issue with Matlab and not with the network architecture and/or the OSes involved?
michael adams
le 7 Juil 2025
works in every other environment. Matlab is the only tool that produces this problem.
Rik
le 7 Juil 2025
What code are you using? Did you contact technical support?
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

