简体   繁体   中英

Automating reading, treating and saving .txt files in a directory using Matlab

I have a directory with two folders: one folder contains several subfolders with multiple .txt files (folder 1). The second folder contains several .hjson files (folder 2).

I would like load each .txt and .hjson files make a several calculation (eg velocity, acceleration, curvature) and save in the same .txt file adding news columns with headers (eg velocity, acceleration, curvature). So far, I have one code to load .txt files.

My goal is to make a code that reads, loads, computes and automatically save it. Please let me know if you have any suggestion.

%% Read and load 
dir_to_search = 'C:\Programs\pedro\Test\';
txtpattern = fullfile(dir_to_search, '*.txt');
dinfo = dir(txtpattern);
for K = 1 : length(dinfo)
   thisfilename = fullfile(dir_to_search, dinfo(K).name);  %just the name
  thisdata = load(thisfilename); %load just this file
End

You can use fprintf to write to a file. For example:

formatSpec="%9.5f  %8.5f\n";
for i=1:n
    fprintf(fid,formatSpec, var1(i),var2(i));
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM