David
David

Reputation: 25

How to read data one by one from files into matlab

I have a file which is like this:

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

You can see: There are two kinds of object in here, P and T. There are 9 P s and 10 Ts. There are 4 kinds of information about P, 1 integer and 3 floating point numbers. There are 6 kinds of information about T, 3 integers and 3 floating point numbers.

Now I need the matlab program knows how many P and T is there, and then read every kind of information about P into a 1-D array, and every kind of information about T into a 1-D array.

So there will be 4 column arrays to store P information. and 6 column arrays to store T information.

Based on my knowledge, I write the following program. And it doesn't work, no need to say. Please help me out.

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

error message:

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

Upvotes: 2

Views: 72

Answers (1)

bern
bern

Reputation: 366

I was able to get two structures Ttmp and Ptmp, which are 1D cell arrays, each cell array contains the data from a single column. These cell arrays are then turned into regular vectors

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

Upvotes: 1

Related Questions