0

[MATLAB]

I have a text file that is a list of numbers. Below is a sample but my actual file is a list of thousands of values, each on a new line.

0.01080000000

0.00720000000

0.05760000000

0.00360000000

How do I loop through this text file and input the data into a matrix of the size x = 431 and y = 415? Again, text file just has a list so every 431st number I need to go to a new row in my matrix.

clear;

%Load in text file
filename = 'Water_1973_points.txt';
T = fopen(filename);

%Count number of points in x,y (x = 431) (y = 415)
xsize = 431;
ysize = 415;

m=zeros(xsize, ysize);
tline = fgetl(T);
for k = 1:length(T)
    for h = 1:xsize
     for j = 1:ysize
         m(h,j) =  k*255;
     end
    end
end

1 Answer 1

1

If your file is that simple it's easy enough to just use importdata and reshape.

For example, using the following Water_1973_points.txt:

1
2
...
10

And

m = importdata('Water_1973_points.txt');
m = reshape(m, 5, 2).';  % Transpose because MATLAB is column-major

Returns:

m =

     1     2     3     4     5
     6     7     8     9    10
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.