【问题标题】:Load only a column of file MATLAB仅加载文件 MATLAB 的一列
【发布时间】:2011-03-07 00:01:01
【问题描述】:

我只对文件的一列感兴趣,(我无法正常加载文件,行的列大小不同)

所以

>load('file.txt');  

不工作,但我想检索该文件中的第一列

【问题讨论】:

    标签: matlab file-io


    【解决方案1】:

    使用 textscan 加载它并使用星号跳过其他列。

    fid = fopen('file.txt');
    textscan(fid, '%*s%*s%s');  % loads only the third column
    fclose(fid);
    

    这假设您的文件中正好有三列。如果您有更多列,您将需要:

    fid = fopen('file.txt');
        twocols = textscan(fid,'%*s%*s%s%*[^\n]');
    fclose(fid);
    

    【讨论】:

    • 所以>> fid = fopen('spamtrn.txt'); >> textscan(fid, '%s'); >> fclose(fid); 只会读取第一列?
    • 不,您需要为每一列输入一个条目。因此,在我的示例中,我假设您有 3 列字符串,而您只想要最后一列。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    相关资源
    最近更新 更多