MATLAB: Best way to extract data from text files and convert rows to vectors

fopenfprintftxt

  • Im having a lot of issues trying to extract the data from the attached text file. My goal is to convert every row in the text file to a 1xn vector where n is the number of variables in the text file row. I also need to make sure that every value extracted into these vectors is an 8 byte floating point. This is what I have so far but I don't know how to convert what I currently have as the output into a matrix. Also is there a way that I can only extract say the first 4 rows instead of all of them?
clc
clear all
fid = fopen('scratch.txt');
tline = fgetl(fid);
while ischar(tline)
disp(tline)
tline = fgetl(fid);
end

Best Answer

fid = fopen('scratch.txt');
line_contents = {};
count = 0;
tline = fgetl(fid);
while ischar(tline)
if ~isempty(deblank(tline))
count = count + 1;
line_conents{count} = sscanf(tline, '%f');
end
tline = fgetl(fid);
end