【问题标题】:Loop through .txt files in a folder and search for certain characters in the file in MATLAB循环遍历文件夹中的 .txt 文件并在 MATLAB 中搜索文件中的某些字符
【发布时间】:2014-10-22 06:27:01
【问题描述】:

如何遍历文件夹中的.dbl and.txt 文件,找到具有特定编号和条件(例如“激光开启”)的.txt 文件,然后对关联的.dbl 文件执行操作(如果名字相似)?

我真的没有太多,我似乎无法使用getfield(files, 'name') 访问我想要查看的文件名,所以我真的被卡住了。这是我到目前为止所掌握的内容,以便为我的问题提供更多结构。

% specify folder with the load function to manipulate .dbl files
folder = 'some folder';

% specify folder that has the data
folder2 = 'some other folder';

cd(folder);
addpath(folder2);

% specify parameters implemented in data collection program
start_delay = 0; % in ps
step_size = 20; % in ps
n_steps = 30;

% loop through folders
files = dir(folder2);
for i = 1:size(files,1)
    if i == '*.txt
    % find string 'ON'
    % find a number in .txt file
    % for .txt files with string 'ON', look for .txt files for delay =0, then 20, then 40, etc.
    % find associated .dbl file 
    % manipulate .dbl file
    end
end

【问题讨论】:

    标签: matlab file text


    【解决方案1】:

    您正在寻找的东西是*.xxx 命令和dirstrfind,看看这个例子:

    txtList=dir('*.txt');
    sblList=dir('*.dbl');
    
    for ii=1:length(txtList)
    
        fid=fopen(txtList(ii).name); 
        C= textscan(fid,'%s'); %depends on your formating
        C=C{1}{1}; %supposing you .txt is really just one string
        fclose(fid);
    
        if (sum(strfind(C,'ON'))) %for .txt files with string 'ON'...
    
            Equals0=strfind(C,'0'); % ... look for =0,...
            Equals20=strfind(C,'20'); %then =20
            %... etc
    
        end
    
        %you get the idea...
        %do whatever you want with dblList
    
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-07
      • 1970-01-01
      • 1970-01-01
      • 2019-05-05
      • 1970-01-01
      • 2019-09-03
      • 2014-04-24
      • 1970-01-01
      相关资源
      最近更新 更多