0
votes

I've got a .txt file with always two values per line separated by tabs:

0   0
23  69
45  108
81  158
110 253
125 357
141 492
165 606
179 753
189 983
.   .
0   0
4   31
33  38
45  89
60  115
75  166
93  201
107 216
116 291
133 366
148 480
170 631
196 720
207 994
.   .
0   0
19  81
33  102
46  128
72  161
138 236
178 398
197 537
210 658
220 832
.   .
0   0
24  38
40  90
71  166
99  193

etc.

Always beginning with 0 0 and ending with . . (80 times)

I'd like to read all the data in with Matlab. This is my code:

% Variab
line{1} = 0;
% Open files
fid = fopen(('D:\Dokumente\Studium\8. Semester\BA\Vali mit einzelenne punkten\alle.txt'), 'rt');
% Read Data
for i = 1:80
    j = 1;
    line = fgets(fid);
    line = textscan(line,'%f %f');
    while line{1} ~= '.'
        digNum{i}(j) = line{1};
        gewicht{i}(j) = line{2};
        line = fgets(fid);
        line = textscan(line,'%f %f');
        j = j + 1;
    end
end

So as you can see I want the left number to be saved as digNum{upOneValueWhen'.'}{numberIn'Vector'} and the right number gewicht{upOneValueWhen'.'}{numberIn'Vector'}.

Everything works fine but when I get to digNum = 46 (second to the last "vector") {upOneValueWhen'.'} goes up a number. I have no idea why. 'i' should only go up after '.' but for some reason it goes up at this one particular spot.

Any ideas??? Thank you so much in advance

2
Your sample data does not reproduce your issue. - excaza
Do you need me to copy-paste all the data? - Mike-C
What's needed is an example that reproduces the issue. - excaza
This looks weird line = textscan(line,'%f %f'); while line{1} ~= '.'. You cannot read a . as float with textscan. Try textscan('. .', '%f %f');. MATLAB 2015b - patrik
Thinking about it this should not be the root of your problem. It is just weird. I cannot see much else in your code which is wrong. Maybe you had something else in mind when you wrote the code. My idea is that you check the MATLAB debugger and make sure the code does what you want. - patrik

2 Answers

0
votes

I would try to read the entire file in one while loop, line by line, until the end of the file is reached:

% Open files
fid = fopen('test.txt');
% Read Data
i = 1;
j = 1;

while(~feof(fid))
    line = fgetl(fid);

    if strfind(line,'.')
        i = i + 1;
        continue;
    end;

    s = textscan(line,'%s','delimiter',' ');
    digNum{i,j} = str2double(s{1}{1});
    gewicht{i,j} = str2double(s{1}{length(s{1,1})});

    j = j + 1;

end;

fclose(fid);

Now you can easily increment i when a . is recognized.

Furthermore, making use of str2double you would be able to put all the data into two matrices.

I think the above works as mentioned.

-1
votes

As mentioned the data so far does not reproduce the problem. Fortunately you should be able to solve it yourself with these steps:

  1. Place (conditional) breakpoints in your code.
  2. Find the last moment where things are going well, and the first moment where things have gone wrong.
  3. Go to the last moment where things are going well, and run your code line by line, carefully look at how all variables change

This way you will quickly find the problem.