简体   繁体   English

使用Matlab自动读取,处理和保存目录中的.txt文件

[英]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). 我有一个包含两个文件夹的目录:一个文件夹包含多个带有多个.txt文件的子文件夹(文件夹1)。 The second folder contains several .hjson files (folder 2). 第二个文件夹包含几个.hjson文件(文件夹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). 我想加载每个.txt和.hjson文件进行几次计算(例如,速度,加速度,曲率),并保存在同一.txt文件中,并添加带有标题的新闻列(例如,速度,加速度,曲率)。 So far, I have one code to load .txt files. 到目前为止,我只有一个代码可以加载.txt文件。

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. 您可以使用fprintf写入文件。 For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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