Loading File with Interspersed text and numbers

1 回表示 (過去 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 Center および File ExchangeText Files についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by