Convert table and plot dates

24 ビュー (過去 30 日間)
Tim W.
Tim W. 2019 年 6 月 5 日
コメント済み: Peter Perkins 2019 年 6 月 19 日
Hi,
I am kinda struggeling with a simple problem. I am trying plot the data of a table; but i can't get seem to conver the data into numbers and plot them.
The table looks like this.
Date Data1 Data2
________________ _____________ ____________
'12-07-2018 21:45' '-233.05' '-221.68'
'12-07-2018 22:00' '-233.33' '-231.18'
'12-07-2018 22:15' '-232.67' '-227.91'
'12-07-2018 22:30' '-232.95' '-226.35'
'12-07-2018 22:45' '-233.18' '-226.14'
'12-07-2018 23:00' '-233.02' '-228.94'
I tried using str2num etc. but nothing works.
  3 件のコメント
Tim W.
Tim W. 2019 年 6 月 7 日
You are correct, normaly it should give me one row with a datetime and x-number of collums as numbers. The Problem is just that I am having multiple csv files and they all start at the same date, but certain instruments did start their measurments. This means some files have no number for the first thousands rows or so (just the date).
If there is no number in the first row Matlab seems to think the whole collum must be characters.
Peter Perkins
Peter Perkins 2019 年 6 月 19 日
If you are using readtable, use detectimportoptions and force the column to be read in as numeric. A simpler version is to specify the Format parameter.

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

採用された回答

Kojiro Saito
Kojiro Saito 2019 年 6 月 5 日
You can convert cell arrays which contain characters using str2double. Here is a sample code.
% Convert to datetime
data2.Date = datetime(data2.Date, 'InputFormat', 'MM-dd-yyyy HH:mm');
% Convert to double
data2.Data1 = str2double(data2.Data1);
data2.Data2 = str2double(data2.Data2);
plot(data2.Date,data2.Data1,'DisplayName','data2.Data1');
hold on;
plot(data2.Date,data2.Data2,'DisplayName','data2.Data2');
hold off;
legend
  1 件のコメント
Tim W.
Tim W. 2019 年 6 月 5 日
You Sir are my hero,
worked like a charm

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by