dataset arrays - extract and reconstruct variables

8 次查看(过去 30 天)
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?

采纳的回答

Leah
Leah 2013-4-3
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 个评论
Sam
Sam 2013-4-3
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.

请先登录,再进行评论。

更多回答(1 个)

Peter Perkins
Peter Perkins 2013-4-4
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.

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by