Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.
|
|
|
|
Re: why does a cell look different when it is a field in a struct?
Posted:
Dec 5, 2012 1:49 PM
|
|
"matt dash" wrote in message <k9o3ch$o3f$1@newscl01ah.mathworks.com>... > "Nasser M. Abbasi" wrote in message <k9ngld$fsc$1@speranza.aioe.org>... > > Matlab 2012a > > > > I am not good with cells. but this is strange: > > > > --------------------- > > clear all; > > A = {'A' 'a';'B' 'c'}; > > s = struct('data',{A(:,1)'}); > > ------------------------ > > > > Now I typed: > > > > ----------------- > > r1 = {A(:,1)'} > > r2 = s.data > > ----------------- > > > > Isn't the above 2 variable supposed to contain the same thing? > > > > But see: > > > > --------------------- > > r1 = > > {1x2 cell} > > > > > > r2 = > > 'A' 'B' > > --------------------- > > > > I am confused now. Why are they different? > > > > --Nasser > > This has something to do with how the struct function handles cell arrays as inputs. It's basically peeling off that cell that you are putting around A(:,1)'. Note that A(:,1)' is already a cell array, so there's normally no need to put {} around it... but the way the struct function works you need the extra { } to tell it that you want to make a scalar structure (instead of distributing each cell of A to a different index of a non-scalar structure).
And just to clarify: they don't just look different, they are different:
r1= {{'A','B'}} r2 = {'A','B'}
r1{1} = {'A','B'} r2{1} = 'A'
|
|
|
|