【问题标题】:How to extract data from multiple text files with headers?如何从带有标题的多个文本文件中提取数据?
【发布时间】:2015-07-06 10:11:37
【问题描述】:

这是对我之前的问题 (https://stackoverflow.com/questions/31205188/extracting-data-from-multiple-text-files-with-headers) 的回应。

我找到了代码并尝试根据我的文件格式对其进行修改。但是这个程序也不起作用。我已经粘贴了代码和下面的错误。

代码

files = dir('*.n2o');
nume1(files) = 1301;
for k = 1:numel(files)
    fid = fopen('files(k)', 'rt');
    Data = textscan(fid, '%f %f %f %f %f', 'headerLines', 43, 'CollectOutput', true);
    fclose(fid);
    Data= cell2mat( Data);        
end

错误

使用子索引时出错
没有为“struct”类的值定义函数“subsindex”。

trial3 中的错误(第 2 行)
nume1(文件) = 1301;

请帮助我解释这个错误。

【问题讨论】:

    标签: matlab data-extraction


    【解决方案1】:

    第 2 行返回错误的原因是您尝试使用结构进行索引。 “files”是一个结构体,只能用整数索引。

    试试这样的:

    files = dir('*.n20');
    
    for k=1:length(files.name)
        fid = fopen('files.name(k)', 'rt');
        Data = textscan(fid, '%f %f %f %f %f', 'headerLines', 43,'CollectOutput', true);
        fclose(fid);
        Headers(k)= cell2mat(Data);        
    end
    

    【讨论】:

    • 非常感谢您的回复。我尝试了下面提到的评论并且它有效。清除所有文件 = dir('*.n2o'); nfiles = 1301;我的数据 = 单元格(1,nfiles);对于 k = 1:nfiles fid = fopen(files(k).name, 'rt'); N2OData(k,:,:)=textscan(fid, '%f %f %f %f %f', 'headerLines', 43, 'CollectOutput', true); fclose(fid); %N2OData=cell2mat(数据);结束
    猜你喜欢
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多