dataset arrays - extract and reconstruct variables

8 views (last 30 days)
I'm trying to extract variables from multiple dataset arrays and compile them into a single dataset array that retains variable names - very straightforward. My problem is that the resulting array groups all input variables into the first column of the output array, rather than parsing each variable into individual columns. For example...
x = dataset({array1.var, array2.var, array3.var})
... returns a nx1 array with all variables grouped into the first column. Similarly....
varspec = ({array1.var,'var1', array2.var, var2', array3.var, 'var3'})
x = dataset(varspec)
... does the same.
Thoughts?

Accepted Answer

Leah
Leah on 3 Apr 2013
your notation is a little off
x=dataset({array1.var, 'var1'}, {arrray2.var, 'var2'}}
or if var is a string containing the name, then this
x=dataset({array1.var, var1}, {arrray2.var, var2}}
Loren has a very nice blog post on datasets that really helped me
  1 Comment
Sam
Sam on 3 Apr 2013
Thanks, Leah. Yes, I knew my notation was off. I first attempted your suggestion but could not figure out how to break lines in my puny-sized editor window without matlab getting angry. '...' did the trick.
Loren's link is also helpful. Cheers.

Sign in to comment.

More Answers (1)

Peter Perkins
Peter Perkins on 4 Apr 2013
Sam, it may be that you want to combine variables with the same name from different arrays. If that's the case, then something similar to what you're doing is probably the way to go, though I'd do it slightly differently:
x = dataset(array1.var,array2.var,..., 'VarNames',{'var1' 'var2' ...})
But if the variables you're combining already have different names, then I think you'd be better off just using concatenation:
x = [array1(:,'var1') array2(:,'var2') ...]
Hope this helps.

Categories

Find more on Tables 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!