【问题标题】:How to read data one by one from files into matlab如何将文件中的数据一一读取到matlab中
【发布时间】:2015-10-10 07:02:13
【问题描述】:

我有一个文件是这样的:

PoreCount=   9
ThroatCount= 10
0   1.16667 -0.666667   0
1   1.16667 -0.333333   0
2   1.16667  0          0
3   1.5     -0.666667   0
4   1.5     -0.333333   0
5   1.5      0          0
6   1.83333 -0.666667   0
7   1.83333 -0.333333   0
8   1.83333  0          0
0   0   1   0.0610206   0.333333    0
1   0   3   0.0606029   0.333333    0
2   1   2   0.0601841   0.333333    0
3   1   4   0.0612494   0.333333    0
4   3   4   0.0593242   0.333333    0
5   3   6   0.0589063   0.333333    0
6   4   5   0.0599607   0.333333    0
7   4   7   0.0595583   0.333333    0
8   6   7   0.0591209   0.333333    0
9   7   8   0.0601974   0.333333    0

你可以看到: 这里有两种对象,P 和 T。 有 9 个 P 和 10 个 T。 P有4种信息,1个整数和3个浮点数。 T的信息有6种,整数3种,浮点数3种。

现在我需要matlab程序知道有多少个P和T,然后将P的各种信息读入一维数组,将T的各种信息读入一维数组。

所以会有4个列数组来存储P信息。和6列数组来存储T信息。

根据我的知识,我编写了以下程序。而且没用,不用多说。请帮帮我。

prompt = 'Please enter the file name: ';
FileName = input(prompt, 's');
SaturationFile=fopen(FileName);

PoreCount  =fscanf(SaturationFile, 'PoreCount  = %d\n', 1);
ThroatCount=fscanf(SaturationFile, 'ThroatCount= %d\n', 1);

% P =fscanf(SaturationFile, '%d%f%f%f\n', PoreCount);
% P=zeros(4, 1);
PI=zeros(PoreCount, 1);
PX=zeros(PoreCount, 1);
PY=zeros(PoreCount, 1);
PS=zeros(PoreCount, 1);

% T =fscanf(SaturationFile, '%f', [ThroatCount, 6]);
TI=zeros(ThroatCount, 1);
Tb=zeros(ThroatCount, 1);
Te=zeros(ThroatCount, 1);
TA=zeros(ThroatCount, 1);
TL=zeros(ThroatCount, 1);
TS=zeros(ThroatCount, 1);

for i=1:PoreCount
%     P=fscanf(SaturationFile, '%d');
%     PI(i)=P(i, 1);
%     PX(i)=P(i, 2);
%     PY(i)=P(i, 3);
%     PS(i)=P(i, 4);
    PI(i)=fscanf(SaturationFile, '%d'  , 1);
    PX(i)=fscanf(SaturationFile, '%f'  , 1);
    PY(i)=fscanf(SaturationFile, '%f'  , 1);
    PS(i)=fscanf(SaturationFile, '%d\n', 1);
end

for i=1:ThroatCount
    TI(i)=fscanf(SaturationFile, '%d'  , 1);
    Tb(i)=fscanf(SaturationFile, '%d'  , 1);
    Te(i)=fscanf(SaturationFile, '%d'  , 1);
    TA(i)=fscanf(SaturationFile, '%f'  , 1);
    TL(i)=fscanf(SaturationFile, '%f'  , 1);
    TS(i)=fscanf(SaturationFile, '%d\n', 1);
end

错误信息:

In an assignment  A(I) = B, the number of elements in B and I must be the same.
    Error in NetworkSaturationPlot (line 29)
        PI(i)=fscanf(SaturationFile, '%d'  , 1);

【问题讨论】:

  • “不起作用”是什么意思?如果它产生错误,请edit您的问题包含错误。
  • 谢谢,我添加了错误信息,我不明白为什么PI(i),标量会有维度问题

标签: arrays matlab file input line


【解决方案1】:

我能够得到两个结构 Ttmp 和 Ptmp,它们是一维元胞数组,每个元胞数组包含来自单个列的数据。然后将这些元胞数组转换为正则向量

clear all;close all;clc

SaturationFile=fopen('./data.txt');
PoreCount  =fscanf(SaturationFile, 'PoreCount  = %d\n', 1);
ThroatCount=fscanf(SaturationFile, 'ThroatCount= %d\n', 1);
Ptmp=textscan(SaturationFile,'%u %f %f %u',PoreCount);    
Ttmp=textscan(SaturationFile,'%u %u %u %f %f %d',ThroatCount);

PI=Ptmp{1};
PX=Ptmp{2};
PY=Ptmp{3};
PS=Ptmp{4};


TI=Ttmp{1};
TB=Ttmp{2};
TE=Ttmp{3};
TL=Ttmp{4};
TA=Ttmp{5};
TS=Ttmp{6};
fclose(SaturationFile);

【讨论】:

  • P=textscan (...) 工作,并将其读入单元矩阵,但我不能使用 cell2mat,因为它需要矩阵内容是相同的类型
  • 另一种选择是获取 textscan 的输出,并在 for 循环中获取 P 和 T 的内容(在每个元素处)并将它们放入数组中。这有意义吗?
  • 我相信一一阅读是更好的选择。但是怎么做呢?看,其实我是想一一读的。但我的语法似乎不合适。
  • 我已经编辑了答案以包含循环来创建数组 P&T,如果这就是你要找的,请告诉我
  • 谢谢!这个有错误:下标分配尺寸不匹配。 NetworkSaturationPlot 中的错误(第 11 行)PI(jj,:)=Ptmp{jj};我不知道这句话是做什么的!我查了数据,Ptmp读入4个cell,每个cell是列向量,Ttmp读入6个cell,每个cell也是列向量。现在我需要做的是将每个单元格放入单独的列向量中。例如,PI PX PY PS 代表毛孔,TI TB TE TL TA TS 代表喉部
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-06
  • 1970-01-01
  • 2014-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多