필터 지우기
필터 지우기

dataset arrays - extract and reconstruct variables

조회 수: 7 (최근 30일)
Sam
Sam 2013년 4월 3일
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