save tic toc in duration style

36 Ansichten (letzte 30 Tage)
nirwana
nirwana am 19 Jun. 2024
Bearbeitet: Stephen23 am 20 Jun. 2024
Hi all, I would like to save my computational time using tic toc but in format duration (i am trying to use duration but I don't know how to save it).
Here is my code, so far I just get the time in second, and exact time give me number separate by comas. What I want to get is something like
1 hour 20 second saving in txt file. Do you guys can help me?
close all, clc, clear all
tStart = tic;
a=load('PE_201507_LPF7Hz_m5L3.dat');
plot(a)
tEnd = toc(tStart)
tEnd = toc(tStart);
time_exec=datevec(tEnd./(60*60*24)) %change time in tictoc to
name=sprintf('exec_time.txt')
dlmwrite (name,time_exec);
  1 Kommentar
Rik
Rik am 19 Jun. 2024
Do you have a specific format in mind for the txt file? For now, I will write an answer that should be close to what you want.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Stephen23
Stephen23 am 19 Jun. 2024
tEnd = 60*60+20
tEnd = 3620
txt = mytime(tEnd)
txt = '1 hour 20 seconds'
writelines(txt,'myfile.txt')
Checking:
type myfile.txt
1 hour 20 seconds
function str = mytime(toa)
spl = nan(1,4);
dpf = 100;
toa = ceil(toa*dpf)/dpf;
spl(4) = rem(toa,60); % seconds
toa = fix(toa/60);
spl(3) = rem(toa,60); % minutes
toa = fix(toa/60);
spl(2) = rem(toa,24); % hours
toa = fix(toa/24);
spl(1) = toa ; % days
idt = spl~=0 | [false(1,3),~any(spl)];
idp = spl~=1 & idt;
fmt = {' %d day',' %d hour',' %d minute',' %g second';'s','s','s','s'};
str = strtrim(sprintf([fmt{[idt;idp]}],spl(idt)));
end

Rik
Rik am 19 Jun. 2024
The code below removes the cargo cult programming, removes the sprintf where you aren't actually using it to format a string, and replaces dlmwrite by code intended to write a text file.
I separated out the code to convert a number of seconds to a char vector. It is far from optimal, but since it uses the duration class, it is fairly easy to change to suit your needs. Note that this skips a time element if it is 0 (so it will reproduce your example of 1 hour and 20 seconds).
tStart = tic;
a=load('PE_201507_LPF7Hz_m5L3.dat');
plot(a)
tEnd = toc(tStart);
fid = fopen('exec_time.txt','w');
fprintf(fid,'%s',SecondToTimeStr(tEnd));
fclose(fid);
function str=SecondToTimeStr(sec)
% This function takes a scalar containing the time in seconds.
% The returned value is a char vector.
D = duration(0,0,sec);
str = '';
if hours(D)>0
plural = ''; if hours(D)>1,plural = 's';end
str = [str sprintf('%.0f hour%s',hours(D),plural)];
D = D - hours(floor(hours(D)));
end
if minutes(D)>0
if ~isempty(str),str = [str ', '];end
plural = ''; if minutes(D)>1,plural = 's';end
str = [str sprintf('%.0f minute%s',minutes(D),plural)];
D = D - minutes(floor(minutes(D)));
end
if seconds(D)>0
if ~isempty(str),str = [str ', '];end
plural = ''; if seconds(D)>1,plural = 's';end
str = [str sprintf('%.0f second%s',seconds(D),plural)];
end
end
  8 Kommentare
Steven Lord
Steven Lord am 20 Jun. 2024
@Rik Making the Format property for duration objects more flexible (especially with respect to literal characters) like the Format property for datetime objects seems like a reasonable enhancement request.
dt = datetime('today', Format = "'It is' eeee, MMMM d, yyyy")
dt = datetime
It is Thursday, June 20, 2024
It probably wouldn't have the exact syntax you showed (fprintf-style format specs aren't always the easiest piece of functionality to understand) but if you could specify the duration format as something like
fmt = "d 'days', h 'hours'"
fmt = "d 'days', h 'hours'"
would that satisfy the use cases you have in mind?
@Stephen23 Having du.minutes [property access] and minutes(du) [method call] return different things 1) may not be possible without perhaps careful overloading of both types of indexing and 2) sounds like a strong source of confusion going forward. We have enough trouble at times with functions that are named similarly but not identically! [Example: one of the functions findstr and strfind is listed as discouraged on its documentation page. Without checking, can you tell me which one?]
Having a property named minute and a method named minutes is technically possible, but again is likely to require some thought and/or checking the documentation each time you use them to determine which one you want to use. I can think of many cases where we have function names that differ by one extra character but usually those "just plain work" in common cases where you pass the inputs valid for one into the other (eig and eigs is one example) or the meaning of the extra character is IMO easy to understand and/or remember in context (sin for radian-based sine, sind for degree-based sine.)
Stephen23
Stephen23 am 20 Jun. 2024
Bearbeitet: Stephen23 am 20 Jun. 2024
@Steven Lord: for the properties I would stick to the singular, just like DATETIME does.
Or if confusion with the methods is a concern, then the properties could be simply d, h, m, s.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by