フィルターのクリア

Reading specific data from a txt/csv file

10 ビュー (過去 30 日間)
Hissam Aziz
Hissam Aziz 2013 年 3 月 27 日
回答済み: Seyedali Pourmoafi 2020 年 10 月 25 日
Hi Everyone, i'm relatively new to Matlab and this is my first post on Mathworks! Wohoo! Will be grateful for any help!
So i'm trying to read data generated from a device which measures the energy consumption. When the csv/text file is generated it looks like this:
03/27/2013 11:20:46.000000000 PM;0.46957; 0.00002
03/27/2013 11:20:46.000083201 PM;0.45180; 0.00000
03/27/2013 11:20:46.000166402 PM;0.45180; 0.00007
My aim is to read "11:20:46.000000000" as a time stamp (the hour can be removed, all the need are the minutes and seconds" and "0.46957" as the power consumed, continuously for each row. This data will eventually be plotted and after curve fitting an integral.
I'm not sure how to how to achieve this. How do i read the time, and the energy? How do i read the time so that it can be considered as data and plotted? The file is huge, around 300 Megabytes. I can read simple csv files sure, but this file is structured strange and generated from C++ and i'm not sure i can change anything within the code, nor am i allowed to make any changes :S so the csv file will stay as is. Any help would be greatly appreciated! Thank you!

採用された回答

Matt Tearle
Matt Tearle 2013 年 3 月 27 日
編集済み: Matt Tearle 2013 年 3 月 27 日
If you have a recent version of MATLAB, you can use the Import Tool to do it interactively, then generate the code. Or you can jump straight to the joy of the textscan function!
Do you need the dates at all? It sounds like the only time information you want is minutes and seconds. So:
fid = fopen('filename.txt');
rawdata = textscan(fid,'%*s %*f %f %f %*s %f %*f','delimiter',{' ',';',':'},...
'MultipleDelimsAsOne',true);
times = rawdata{1} + rawdata{2}/60;
powconsumed = rawdata{3};
fid = fclose(fid);
This treats the file as a string (the date) followed by a space delimiter, followed by three numbers separated by a : delimiter, followed by a space delimiter and another string ("PM"), then two numbers separated by a ; delimiter. The * characters in the format string tell textscan to ignore those fields, so it just reads the minute, second, and power fields. I'm also converting minutes and seconds to a decimal minute (minute + second/60). You can change that to whatever you want -- keep them separate if you want.
  4 件のコメント
Debjit Pal
Debjit Pal 2013 年 3 月 29 日
Possibly before you store you can execute
>> format long
That will do the job I guess
Matt Tearle
Matt Tearle 2013 年 4 月 1 日
There's an important distinction between how MATLAB stores numbers and how they are displayed. But default, numbers are stored as double precision in MATLAB (about 16 decimals) but displayed to the Command Window to 4 d.p. If you use %f in textscan, you'll get doubles. If you want to see more decimals in the display, you can use the format command to set the default display format, as Debjit Pal suggests, or use something like fprintf to print values out to a fixed format.

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

その他の回答 (1 件)

Seyedali Pourmoafi
Seyedali Pourmoafi 2020 年 10 月 25 日
Hi everyone,
I want to read so specific data from a CSV file and there are not numeric formats, there are some names. could you please help me in this regard? How can I do it?

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by