Read data from text file

2 ビュー (過去 30 日間)
Edward
Edward 2013 年 9 月 26 日
コメント済み: Matt Kindig 2013 年 9 月 26 日
Hi, i have a text file with contents:
26/09/2013,16:04:40 2412 928.0 49.94930
25/09/2013,14:24:30 2412 914.0 -999999
The file above contains a date, time and 3 numbers per line.
I want to read this into an array of dates, times, and x, y, z for the numbers where each element in the array is each line of the file.
Is there an easy way to do this (if so how?) or do i have to read the line as a String and parse it?

回答 (1 件)

Matt Kindig
Matt Kindig 2013 年 9 月 26 日
編集済み: Matt Kindig 2013 年 9 月 26 日
You should be able to do it with textscan
fid = fopen('/path/to/your/file.txt', 'rt');
output = textscan(fid, '%s %s %f %f %f', 'Delimiter', ' ,');
fclose(fid);
%you can then break this out into individual variables
dates = output{1}; %you can further use datenum() to convert to a date number
times = output{2};
x = output{3};
y = output{4};
z = output{5};
  2 件のコメント
Edward
Edward 2013 年 9 月 26 日
This is brilliant, i want the output as a structure so for example, struct.x, struct.y etc how do i make it so i output an array of structures struct(2) rather than a structure of arrays struct.x(2)
I hope this makes sense!
Matt Kindig
Matt Kindig 2013 年 9 月 26 日
Some of the basics of working arrays of structures (and structures of arrays) can be found here: http://blogs.mathworks.com/videos/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/

サインインしてコメントする。

カテゴリ

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