MATLAB: Reading only numeric data in a text file

fscanf

I have a formatted text file (attached). I have to read it, ignoring some bits like the headings. After reading I should be able to get tx=2
ty=18
and the 4×4 matrix which is there in the text file. Rest of the strings like the headings, sensor1, chirp1 etc must be ignored.
How can I do this?

Best Answer

it's almays more complicated to read a file with no defined data structure.
In your case, you can use (and adjust for other files)
% read the file
fid = fopen('FrequencyShiftsV1.txt','r');
MyTextFile = textscan(fid,'%s','delimiter','\n');
fclose(fid);
MyTextFile = [MyTextFile{:}];
% get and evaluate the lines 2 and 3 (matlab expression).
eval(MyTextFile{2});
eval(MyTextFile{3});
% extract the part containg the matrix to read
MatrixLines = MyTextFile(7:end);
% get rid of Chirp1, Chirp2, ...
MatrixLines = regexprep(MatrixLines,'Chirp[^%d]','');
% Convet the char into a numerical matrix.
MyMatrix = str2num(cell2mat(MatrixLines));