I need a little help with reading from file.
3 ビュー (過去 30 日間)
古いコメントを表示
There's a file containing the coordinates of 10 people at different time (time goes from 1 to 200). The file looks like this:
1
t 1 2 3 ... 200
x 0 -1 -2 ... -11
y 0 1 2 ... 0
2
t 1 2 3 ... 200
x 0 0 1 ... -1
y 0 0 1 ... -3
.
.
.
10
t 1 2 3 ... 200
x 0 0 -1 ... -6
y 0 1 0 ... 7
1-10 are the numbers of the people.
t and the numbers from 1 to 200 are the time stamps.
x is the x-coordinate, y is the y-coordinate (these are random).
I have tried different ways to read the file, but none of them worked. I need to be able to access each number and the t,x,y characters as well (by access, I mean to display the number/character, etc).
How can I read a file like this? Does it require some sort of hybrid format specifier?
The code to call the function, and under it is my try to read from the file:
clear all;
filename='gyak08_szorg_data1.txt';
my_t=3;
my_x=1;
my_y=-1;
adat = talalkozasok(my_t, my_x, my_y, filename)
function adat = talalkozasok(my_t, my_x, my_y, filename)
fib=fopen(filename);
adat = fscanf(fib, '%c\n');
end
6 件のコメント
Dyuman Joshi
2022 年 4 月 28 日
You can use import tool to import data from your file and proceed further.
採用された回答
dpb
2022 年 4 月 28 日
編集済み: dpb
2022 年 4 月 28 日
That's an annoyingly difficult form to parse -- looks nice to read, but the mixture of text and data and different number variables per record makes it very irregular to parse.
Simplest thing I found was something like
data=readcell('gyak08_szorg_data1.txt'); % just bring in as Nx1 cell array
t=str2double(split(strrep(data(2),'t ',''))); % t is same all records, just read one
x=str2double(split(strrep(data(3:4:end),'x ',''))); % parse the x, y data
y=str2double(split(strrep(data(4:4:end),'y ','')));
If there were different times for some files, then just read the full array same as for x, y.
In future, create more regular, easily parsed data file formats!!! :)
2 件のコメント
dpb
2022 年 4 月 28 日
This probably won't go over well with instructor of introductory MATLAB course, plus, of course, it isn't your own work...if had noted it being classwork I'd have given hints, not full solution. :)
One can, of course use fscanf and/or fgetl and parse the file in conventional manner; depending upon just what you have covered and your instructor's penchant for how to approach things one could attack it in several ways.
The typical problem one runs into with such formats with fscanf or textscan in these situations is the variable line length that the file pointer runs off into never-never land when it fails on the conversion if not given a counted number of fields to parse. The way around that is to count delimiters or have a field in the file that can be read to know how many fields are expected but this file format doesn't have such -- part of the poor design.
Hence, if it were me, I'd probably opt for the fgetl solution and parse each line as I read it...
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で String Parsing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!