how to convert a timetable to structure of timetables

8 views (last 30 days)
hi, I'm looking for some pointers/advice on how best to move data from a timetable to a structure contataining individual variable timetables.
TT = Time, var1 , var2, var3, etc
to
struct containing Time,var1 ; Time var2; Time var3; etc...
thanks in advance

Accepted Answer

Guillaume
Guillaume on 2 Feb 2020
Edited: Guillaume on 2 Feb 2020
table2struct(timetable2table(yourtimetable))
%or possibly
table2struct(timetable2table(yourtimetable), 'ToScalar', true)
However, tables and timetables are a lot easier to work with than structures, so it's a bit odd to want to do that. Can you explain the reason behind it?
  4 Comments
john kozicz
john kozicz on 3 Feb 2020
Guillaume, once again; thanks for your timely help with this. it is much appreciated.
i'm not that familiar with anonymous functions/notation so i'm wondering is the fn in @(fn) refering to the variable 'fields' in the call to cellfun(@(fn)...,fields,....) ?
i don't have access to the legacy code (yet!) so can't make the necessary changes to the old code to accept timetables as input; on the brighter side i've learned someting that i would not have had to otherwise ie how to use cellfun.
best regards
Guillaume
Guillaume on 3 Feb 2020
fn is indeed the input variable of the anonymous function that will receive in turn each element of the cell array fields. All cellfun is doing is iterating over each variable of the timetable to return only that portion of the timetable. This is all concatenated as the 2nd row of a cell array, with the first row the actual variable names.
Expansion of cell arrays into a comma separated list allows us to pass that as inputs to the struct function.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!