- read the excel data as tables with readtable. Since R2013b, there's hardly any reason to use xlsread anymore since readtable is more powerful and usually simpler to use.
- convert the tables into timetables, using array2timetable (needs R2016b)
- synchronise your timetables with synchronize and the 'intersection' option.
How to export numbers and date/times in a matrix?
1 view (last 30 days)
Show older comments
I have excel files that contains numbers and date/times. In my case I want to export a new matrix that contains values of 3 variables and their common time in separated columns. First I xlsread the excel file and then I identified each variable . I used the command 'intersect' to find common times between variables. My problem is when I put all together in a matrix ,time's column is represented as a number and not as date-time. When I use datestr(time) ,it gives me right the time's column but the rest values of variables convert to ASCII characters. As follow you can see part of my program:
%Create matrix for each variable (value+time)
SP=[dsp tsp];
HR=[dhr thr];
MAP=[dmap tmap];
ICP=[dicp ticp];
%Intersection of the three variables(sp,hr,map) in order return values and common times
[C1,ISP,IHR] = intersect(SP(:,2),HR(:,2));
[C2,IC1,IMAP] = intersect(C1,MAP(:,2));
final_map=MAP(IMAP);
final_hr=HR(IHR);
final_sp=SP(ISP);
time2=datenum(C2);
time3=datestr(time2);
final=[final_hr final_sp final_map time3];
Any help is accepted!:)
0 Comments
Answers (1)
Guillaume
on 8 Feb 2017
Edited: Guillaume
on 8 Feb 2017
Assuming you're on R2016b, the simplest way to solve your problem is to use the modern tools that have been implemented in recent versions:
All in all, your code should be only four lines:
SP = array2timetable(readtable('somefile.xlsx', 'somerange'));
HR = array2timetable(readtable('somefile.xlsx', 'someotherrange'));
MAP = array2timetable(readtable('somefile.xlsx', 'anotherrange'));
result = synchronize(SR, HR, MAP, 'intersection') %merge all three tables into one keeping only times common to all three.
3 Comments
Guillaume
on 9 Feb 2017
If I understood correctly, then again readtable would be the easiest way to do what you want:
t = readtable('somefile.xlsx', 'B:D'); %read excel file ignoring column A
newt = unstack(t, 2, 1); %extract each variable in columns
newt(any(isnan(newt{:, 2:end}, 2)), :) = []; %remove rows which don't have common time for all variables
See Also
Categories
Find more on Spreadsheets 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!