A Script Snippet to Go Through A Directory

In order to avoid repeat operations, I need to write a script to do the repeated word. Here is a snippet using in Matlab. This post is just as a
backup.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Code Name:
% Coder: Author
% date: 2019-10-09 %C-t
% description:
% Input module:
% Output data:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear;
clc;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Setting the File Path
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

path='.../date_initial/';
dir_dat = dir(strcat(path,'*.dat'));

% cd to save directory
cd('.../date_final');

% number of files
file_number = length(dir_dat);

% name list of files with string type
file_name_arr = string ( {dir_dat.name}' );


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Loop to read, process and save data files
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for loop_idx = 1:file_number

% # read in data
file_address = strcat( path, file_name_arr(loop_idx) );
data = dlmread( file_address );

% # process data



% # out_put data plot if necessary
% save data with name rule

out_put= ;
out_name = strcat( file_name_arr(loop_idx), '_susceptibility', '.dat')
save(out_name,'out_put','-ascii','-double');
end