代码之家  ›  专栏  ›  技术社区  ›  Zynk

在MATLAB中的脚本中将数据作为单元格数组导入

  •  0
  • Zynk  · 技术社区  · 10 年前

    我需要将数据作为单元格数组导入到Matlab中,以获取许多文件,然后再对它们执行一些操作。是否有任何方法可以在脚本中导入数据以自动而不是手动执行?

    我手动执行的操作是:

    -主页>导入数据

    -选择.txt文件

    -作为单元格数组,2列作为文本。

    -列分隔符:逗号。

    更新: 这里有一小段我的.txt文件:

    /home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/d-0197.bmp, [329 210 50 51]
    /home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/c-0044.bmp, [215 287 59 48]
    /home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/e-0114.bmp, [298 244 46 45]
    /home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/102.bmp, [243 126 163 143]
    /home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/e-0120.bmp, [250 400 48 48]
    

    结果:具有2个文本列的单元格数组。

    -第一列包含图片的路径:

    '/home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/d-0197.bmp'
    '/home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/c-0044.bmp'
    '/home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/e-0114.bmp'
    '/home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/102.bmp'
    '/home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/e-0120.bmp'
    

    -第二列包含文本形式的数组:

    ' [329 210 50 51]'
    ' [215 287 59 48]'
    ' [298 244 46 45]'
    ' [243 126 163 143]'
    ' [250 400 48 48]'
    

    非常感谢。

    2 回复  |  直到 10 年前
        1
  •  1
  •   Luis Mendo    10 年前

    您可以使用 importdata 。这给出了不同单元格中的每一行。然后使用 regexp 使用 'split' 选项:

    y = importdata('filename.txt');
    y = regexp(y, ',', 'split'); %// or ', '
    y = cat(1, y{:});
    
        2
  •  1
  •   articuno    10 年前

    试试这个-

    T = readtable('fileName.txt','Delimiter',',','ReadVariableNames',false)