[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)
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;

Answers (1)

Jan
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
  1 Comment
Ilan Boulet
Ilan Boulet on 21 Jun 2022
Thank you a lot for your answer Jan, I'm gonna try to implant your script into mine.
What I want to do now it's to plot a 3D figure, with 1 file = 1 point in absciss, the Vg in ordinate and the intensity in z (color map) (the Vg and the intensity are given into the .txt files on respectively column 1 and column 4).
Here's the end of the program :
dataup=DATAS(1:101,:);
datadown=DATAS(102:end,:);
Vgup=linspace(-40,40,101);
Vgdown=linspace(40,-40,101);
time=(t-t(1,1))*86400;
figure
surf(time,Vgup,dataup);
title('Sweep up','FontSize',20),view([0 0 1]),xlabel('Time (s)','FontSize',20),ylabel('Vg (V)','FontSize',20)
set(gca,'FontSize',16)
shading flat
colorbar('FontSize',16)
figure
surf(time,Vgdown,datadown);
set(gca,'FontSize',16)
shading flat
title('Sweep down','FontSize',20),view([0 0 1]),xlabel('Time (s)','FontSize',20),ylabel('Vg (V)','FontSize',20)
colorbar('FontSize',16)

Sign in to comment.

Products


Release

R2007b

Community Treasure Hunt

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

Start Hunting!