I need some help to formatting a data file in matlab.

4 ビュー (過去 30 日間)
Bruno Souza
Bruno Souza 2017 年 12 月 22 日
コメント済み: Bruno Souza 2018 年 1 月 13 日
I need help formatting a data file in matlab. It is for my college research. The file has the pattern below.
[ No ] [ Temp ]
1 1 01:00:00
1 20.00
2 20.00
3 20.00
4 20.00
5 20.00
1 1 02:00:00
1 20.00
2 20.00
3 20.00
4 20.00
5 20.00
....
I would like to know how to store the data this way:
Time No1 No2 No3 ...
00:00:00 20.00 20.00 20.00 (here is the temp in each "no")
01:00:00 20.00 20.00 20.00 (the temp is changing by the time, but it is just an example)
02:00:00 20.00 20.00 20.00
If someone knows how to do this, please help me. My contact is brunojsouza@outlook.com Thank you
  4 件のコメント
Walter Roberson
Walter Roberson 2017 年 12 月 22 日
I do not mean the value of he entries.
You have
1 1 01:00:00
1 20.00
2 20.00
3 20.00
4 20.00
5 20.00
1 1 02:00:00
which has a line with a time, and then exactly 5 entries without a time, and then back to a line with a time. The next section shows exactly 5 entries after the line with the time as well. Is that 5 constant? Will there ever be cases like,
1 1 01:00:00
1 20.00
2 20.00
3 20.00
4 20.00
5 20.00
6 20.00
for example?
If the number after the line with the time is not always 5 lines, then is it the consistent within any one file?
Bruno Souza
Bruno Souza 2017 年 12 月 22 日
Sorry, is always 5! 5 is the number of "NÓ" (in Portuguese means "dot") and the data show the temperature in each dot by hour

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

採用された回答

Walter Roberson
Walter Roberson 2017 年 12 月 23 日
fid = fopen('YourFileName.txt', 'rt');
fgetl(fid); %ignore the header
counter = 0;
while true
timeline = fgetl(fid);
if ~ischar(timeline); break; end %reached end of file
hms = sscanf(timeline, '%*d%*d%d:%d:%d');
counter = counter + 1;
Time(counter) = duration(hms(1), hms(2), hms(3));
No(counter, :) = cell2mat( textscan(fid, '%*f%f', 5) );
end
fclose(fid);
Then,
T = array2timetable(No, 'RowTimes', Time, 'VariableNames', {'No1', 'No2', 'No3', 'No4', 'No5'});
or
T = cell2table( [num2cell(Time), num2cell(No)], 'VariableNames', {'Time', 'No1', 'No2', 'No3', 'No4', 'No5'});
  10 件のコメント
Bruno Souza
Bruno Souza 2017 年 12 月 23 日
yes! thank you!!!
Bruno Souza
Bruno Souza 2018 年 1 月 13 日
If I need to put this table T in a file, How can I do this? (I had to edit and plus more colums, so now It has No1.. until No21. I'll need to join this table with another data later. One file (FileA) is a matrix [744x2] from number one until number 744, and I'll put It in the first colum like "Hora". The other file (FileB) is the Clima, It is a matrix [744x2] too, but are random values. It will be like that:
Hora No1 No2 No3 ... No21 Clima
1 ... ... ... 20.05
2 ... 25.04
3 ... ...
.. ... ...
744 ... 21.09

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by