Now that we have example data, it's possible to give more details. The gist of my answer still stand: import the data as timetable, then one-liner retime to resample the data with whichever time interval you want.
The difficulty with your file is that the actual time/date is split over several variables which is not ideal. In addition, the time column being in 24 hours format but still having a PM indicator for afternon but no AM indicator for morning really complicates things a bit.
opts = detectImportOptions('data.csv'); %have to use import options because of the wonky format of the file
opts = opts.setvaropts(3, 'Type', 'char'); %prevent decoding as duration which would fail because of the AM/PM
data = readtable('data.csv', opts); %import as table
data.(3) = duration(strrep(data.(3), ' PM', '')); %get rid of the useless PM, then convert to duration
data.DateTimeUnified = data.(2) + data.(3) + seconds(data.(4) / 1e6); %convert microseconds to seconds and add the whole lot
data.DateTimeUnified.Format = 'dd MMM yyyy HH:mm:ss.SSSSSS'; %use whichever display format you prefer
data(end, :) = []; %get rid of the last row which is nonsense
data(:, 2:4) = []; %optionally remove original date/time columns
tdata = table2timetable(data, 'RowTimes', 'DateTimeUnified'); %convert to timetable
and then as said, retime to get the data in interval of 30 seconds:
Which version of matlab are you using? It must be a version before setvaropts allowed you to specify the type.
Do what it says then, replace:
opts = opts.setvaropts(3, 'Type', 'char');
by
opts = opts.setvartype(3, 'char');
It's possible you may get more errors if you version is too old.
"Clearly this would have been above my skills to figure out!"
Most of the code is just there to cope with the poor formatting of the time in your file. Had that been written properly as a 24h HH:mm without a sometimes missing PM indicator, a simple readtable would have worked. If the date and time had been combined in just one column, it would have been even easier to read. If you have the opportunity to change the output format of whatever generated the file, I'd encourage you to do so. It would allow you to replace the 9 lines of code by just:
I had noticed that this time column was super wacky, but the data is the output of a software that unfortunately does not let me change anything in how it exports.
I tried the algo you recommend and it goes through without error (victory!) and the output is ALMOST what I need. The number of rows is correct and the states have been repeated correctly.
The but is that the time column, now "dateTimeUnified" does have 30 s increments but does not start at the same time as the original time.
In the original data the start time in first row was "21:18:52" so I need the "new time" go:
newtime = 21:18:52
21:19:22
21:19:52
21:20:22, etc
Is it possible to make the "DateTimeUnified" start from the first time stamp?
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
댓글 수: 1
이 댓글에 대한 바로 가기 링크
https://kr.mathworks.com/matlabcentral/answers/514282-adding-rows-to-matrix-conditionally#comment_818869
이 댓글에 대한 바로 가기 링크
https://kr.mathworks.com/matlabcentral/answers/514282-adding-rows-to-matrix-conditionally#comment_818869
댓글을 달려면 로그인하십시오.