Clear Filters
Clear Filters

Time Format: import unique format to be able to plot

1 view (last 30 days)
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

Steven Lord
Steven Lord on 13 Feb 2018
In the Import Tool, click on the triangle in the column header to select how you want each column to be imported. In the example on this documentation page it's right next to the word Number below each person's name. The default options are Text, Number, Categories, and Dates and Times. The default Dates and Times format won't work for your data, but you can click on "more date formats" to select a different format or enter your own.
If you're planning to read in multiple files all in the same format, once you've finished setting up how you want the file to be read generate a function from the Import Tool as described in the "Import Data from Multiple Text Files" on that documentation page and use that function to import your data from the other files. You can also read the generated code to see how it imported the date and time data, in case you want to reuse that portion of the generated function for some other application.

More Answers (2)

Peter Perkins
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
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
Dylan Mecca on 13 Feb 2018
Thank you for your response, dpb.
Form my application, I will need to stick with the uiimport tool for it is the most efficient and user friendly. I personally will not be using this code regularly so it needs to be very versatile. With that said, I would like to see if you may have a suggestion as far as creating a function that could interpret what data I have. I'm at a loss here because I don'nt know if it is possible to have make a way for matlab to read data from a cell coming in as [hh:mm:sec.sec] and be able to plot it later accordingly.
However, if there is a way to do something like this, I would greatly appreciate your help or suggestion!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!