Loading File with Interspersed text and numbers

조회 수: 2 (최근 30일)
Aditi
Aditi 2011년 7월 4일
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!

답변 (1개)

Harry MacDowel
Harry MacDowel 2011년 7월 4일
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
Jan
Jan 2011년 7월 4일
+1. FSCANF is the best choice to read a text file with varying line contents.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by