无法从文本文件导入数字

时间:2018-10-03 14:28:35

标签: matlab file-io import

我有一个很大的文件,像这样:

  

10,-247,16,0.465829,-11.505979,0.745327,0.000372,-249.809357,0.000596,6.662333,27.868153,0.000000,0.000000;   8,-246,16,2.600302,-66.482079,4.309549,0.003487,-249.589752,0.005758,31.830967,38.629253,0.000000,0.000000;   11,-247,13,6.514969,-170.550919,10.922515,0.013875,-249.463654,0.023174,60.247765,46.340561,0.000000,0.000000;

我想选择每行的第一个数字并将其另存为矢量,依此类推,依次选择每行的第二个数字和第三个数字。

我的代码是这样的:

delimiter = ',';

    ..... sprintf('%s%s_%d.txt',fullDataPath(j,:),soggetti(j,:),s);
                   if exist(filename, 'file') 
                        disp(filename)
                        fid = fopen(filename);
                        fileAllData = importdata(filename, delimiter);
                        fileData = fileAllData.data(:,1:3);
                         x = fileData(:,1);
                         y = fileData(:,2);
                         z = fileData(:,3);
                        fclose(fid);
                        ........
                   else 
                        ..........
                   end
              ...........

错误是这样的:

  

从非结构化数组对象引用结构内容。

我在网上搜索了类似的错误,但是我不明白在这种情况下如何纠正我的代码以及为什么会出现此错误。

1 个答案:

答案 0 :(得分:1)

错误在这一行:

fileData = fileAllData.data(:,1:3);

点索引表示一个结构,但是由于您的文件仅包含数字importdata不能为您提供带有textdata的结构,而只能为data提供结构,意味着您应该正常对其进行索引:

fileData = fileAllData(:,1:3);