I have a matrix of times (HH:MM:SS) : the first column is the start-time and the second is the stop-time. I can extract each columns as:
fileID=fopen('Times.txt');
A = textscan(fileID,'%D %D');
start=A{1};
stop=A{2};
and now I want to create a new column vector with the subtraction between the start-time and the stop-time of each rows. With this new vector I will create a Histogram. So how can I create the difference vector??
Thanks
Michela

6 件のコメント

Andrei Bobrov
Andrei Bobrov 2017 年 7 月 3 日
Michela! Please attach small example your data-file.
Michela Longhi
Michela Longhi 2017 年 7 月 3 日
編集済み: Michela Longhi 2017 年 7 月 3 日
An example is:
start stop
00:12:00 00:12:20
00:12:25 00:12:38
00:13:22 00:14:03
00:14:33 00:15:12
Image Analyst
Image Analyst 2017 年 7 月 3 日
You forgot to attach Times.txt with the paper clip icon. Also, look at the etime() function.
Michela Longhi
Michela Longhi 2017 年 7 月 3 日
Here the file
Jan
Jan 2017 年 7 月 3 日
Do you want to add the column in the file, or only inside Matlab to create the diagram directly?
Michela Longhi
Michela Longhi 2017 年 7 月 3 日
In Matlab

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

 採用された回答

Image Analyst
Image Analyst 2017 年 7 月 3 日

1 投票

How about:
A = readtable('Times.txt', 'Delimiter','\t','ReadVariableNames',false, 'datetime', 'text')
startTimes = A{:,1}
stopTimes = A{:,2}
elapsedTimes = etime(datevec(stopTimes), datevec(startTimes))

1 件のコメント

Michela Longhi
Michela Longhi 2017 年 7 月 3 日
Yes!!! It works well!
Thanks so much
Michela

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

その他の回答 (1 件)

dpb
dpb 2017 年 7 月 3 日

1 投票

I'd make a minor change--
A = cell2mat(textscan(fileID,'%D %D','collectoutput',1));
to convert the cell arrays to an array of datetime. Then as IA noted
E=etime(A:,1),A(:,2));
or
A(:,3)=etime(A:,1),A(:,2));
Or, it could be a good place for the table and you could use
T = readtable('Times.txt','Format','%D %D');
and then you've got the best of both worlds, data as a datenum for use with all its supporting functions such as etime and named identifiers for the variables rather than index expressions or making multiple variables explicitly.
T.Span=etime(T.start,T.stop); % add the elapsed time column to the table
You can also simply add/subtract datetime arrays with arithmetic operators to get another datetime array in "real" time units rather than the duration from etime if that's more suitable for the measure (altho I'm not positive if the histogram functions are aware of it so seconds is probably the better choice there).

3 件のコメント

Michela Longhi
Michela Longhi 2017 年 7 月 3 日
編集済み: Michela Longhi 2017 年 7 月 3 日
Hi @dpb , with
fileID=fopen('Times.txt');
A = cell2mat(textscan(fileID,'%D %D','collectoutput',1));
T = readtable('Times.txt','Format','%D %D');
T.Span=etime(T.start,T.stop); % add the elapsed time column to the table
Matlab give ma an error: Error using cell2mat (line 52) CELL2MAT does not support cell arrays containing cell arrays or objects.
What can I do?
Image Analyst
Image Analyst 2017 年 7 月 3 日
You can call readtable like I did. Did you even see my answer? It works.
dpb
dpb 2017 年 7 月 3 日
Or kept reading mine--I got there eventually! :)
datetime is an object, not a double as we old fogies are used to.

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

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by