[Beginer] How can I change a loop which depends on file's time into a one which depends on name's files ?
1 view (last 30 days)
Show older comments
Hello.
I am new on matlab and programming so maybe it will be an obvious question but here I am:
I found this code on the net to treat the datas I have from my experiments. I tried to understand the code before using it, and as far as I understood, it goes into a specified folder, find every ".txt" files, rank them by date and "put them as one file" (not sure for this actually).
The point is that I would like it to rank them by alphabetical order so I can add manually the time dimension (I note the hour of every step of the experiment).
Could you guys please explain me how exactly the second part of this loop works and if you have some hints to change the "Time Ranking" by alphebetical ranking and include it into the loop it would be amazing !
Also I am not very famailiar with the concept of directory, which seems like to be a basic knowledge so I am working on it.
Thanks a lot !
The code: (I skipped the ploting part)
experience = 'DATAS';
extension ='txt';
filelist = dir([experience,'/*',extension]);
nfiles = length(filelist);
LEGENDS = [];
for ifile = 1:nfiles
disp(['Traitement du fichier n° ',sprintf('%d',ifile)]);
probname = filelist(ifile).name;
probdate = filelist(ifile).date;
tmp=textread([experience,'/',probname],'','headerlines',4)
if ~exist('DATAS')
DATAS = tmp(:,2);
t=datenum(probdate)
else
DATAS = [DATAS tmp(:,2)];
t=[t datenum(probdate)]
end
end
dataup=DATAS(1:101,:);
datadown=DATAS(102:end,:);
Vgup=linspace(-40,40,101);
Vgdown=linspace(40,-40,101);
time=(t-t(1,1))*86400;
0 Comments
Answers (1)
Jan
on 21 Jun 2022
As far as I understand, you want to process a list of files in alphabetical order. This is the order replied by dir(), but unfortunately this is not documented. To be sure here an extra sorting:
experience = 'DATAS';
extension = '.txt'; % Dot added
folder = 'C:\You\Folder'; % Set accordingly
filelist = dir(fullfile(folder, experience, ['/*', extension]);
[~, index] = sort({fileList.name}); % Not needed usually
filelist = filelist(index);
for iFile = 1:numel(filelist)
aFile = fullfile(filelist(iFile).folder, filelist(iFile).name);
... What do you want to do now?
end
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!