Time Format: import unique format to be able to plot
Show older comments
Hello everyone,
I am trying to utilize the time stamp from some data(see attachment) using excel. I want to be able to import the data using uiimport, then change it to a 'double' style variable in the workspace so that it can be plotted against other values from other data. Note: The data being imported to matlab using uiimport appears under a different format than that what you will see in the attachment if you use excel to open it. My problem lies in the case that it gets imported a cell type variable, which because it has text in it, won't be able to be plotted. Using cell2mat command doesn't solve this issue.
When imported to Matlab, the data should read [hh:mm:sec.sec]. I hope someone can point me in a direction that can utilize this somehow that would allow me to plot against other data I have. I appreciate your help, comments and suggestions!!
Accepted Answer
More Answers (2)
Peter Perkins
on 13 Feb 2018
I'm gonna guess that you've stripped away most of the context in the interests of asking a specific question. Usually that's good, but perhaps the bigger picture is worth talking about. I'm gonna guess that your other data are in the same spreadsheet. I could be wrong about all that. In any case, if you have a spreadsheet with multiple columns containing time and data, use readtable to import it:
t = readtable('Book1.csv');
You will get text for those timestamps, because they are not in a format that will be recognized automatically (you could probably use detectImportOptions to tweak that, but it's just as easy to do after the fact).
Your file contains timestamps in the form mm:ss.S (not as hh:mm:sec.sec as you suggest). If those are all on the same day, I suspect you want to convert the text to durations. Unfortunately, you can't do that directly. But it's easy to get there"
t.Time = datetime(t.Time,'InputFormat','mm:ss.S'); % convert text to datetimes
t.Time = timeofday(t.Time); % convert datetimes to durations
t.x = cumsum(randn(size(t.Time))); % pretend this came from the spreadsheet
Now just plot:
plot(t.Time,t.x)

I get that you want to use the Import Tool; still, creating a function from the above may be even simple for others to use.
dpb
on 13 Feb 2018
Convert to datetime type; unfortunately, the uiimport and friends aren't enabled to read time format strings...but, that's easily taken care of...
>> fid=fopen('dylan.csv'); % get a file handle
>> dt=textscan(fid,'%{mm:ss.S}D','headerlines',1); % read as time; is returned as cell of dt
>> dt % is returned as cell of datetime type
dt =
cell
[300×1 datetime]
>> dt=dt{:}; % convert to native datetime array (there isn't a cell2datetime)
>> whos dt
Name Size Bytes Class Attributes
dt 300x1 4815 datetime
>> dt(1:4) % show first few values...
08:18.7
08:18.9
08:19.1
08:19.1
>>
Of course, don't forget to close the file...
fid=fclose(fid);
NB: Looks like your input file is missing the HH field...
1 Comment
Dylan Mecca
on 13 Feb 2018
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!