Loading File with Interspersed text and numbers

1 view (last 30 days)
Hi, I have a .log file that's in the following format:
3 lines of text
5 columns of numeric data (25-30 rows)
3 lines of text
5 columns of numeric data (25- 30 rows)
...and it keeps going for 14 more times.
I want to load this file into a cell array, having each line of the file as one cell. How do I do this?
Any help will be greatly appreciated. Thanks!

Answers (1)

Harry MacDowel
Harry MacDowel on 4 Jul 2011
You want to try fscanf. The documentation: http://www.mathworks.com/help/techdoc/ref/fscanf.html
One of the examples:- % Create a file with temperatures tempstr = '78°F 72°F 64°F 66°F 49°F';
fid = fopen('temperature.dat', 'w+');
fprintf(fid, '%s', tempstr);
% Return to the beginning of the file
frewind(fid);
% Read the numbers in the file, skipping the units
% num_temps is a numeric column vector
degrees = char(176);
num_temps = fscanf(fid, ['%d' degrees 'F']);
fclose(fid);
Try something like that. If there are numbers in between the lines, Try to set a condition which checks before the number and after the number for the type. If it is character before and after then ignore that number. There you go.
  1 Comment
Jan
Jan on 4 Jul 2011
+1. FSCANF is the best choice to read a text file with varying line contents.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!