如何从文件中一个一个读取数据到matlab

How to read data one by one from files into matlab

我有一个这样的文件:

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);

我能够得到两个结构 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);