Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
the data type of ['x']

Subject: the data type of ['x']

From: Yuji

Date: 21 Apr, 2012 03:20:16

Message: 1 of 15

Hi there~

I have a question about the fundamental data type issue.

>> ['xx' 'yy' 1 2 'z']

ans =

xxyyz

What is the data type?

What's the difference between ['xx'] and {'xx'} ?

Is it illegal for a vector to have both char element and num element at the same time?

If somebody could explain this or give me a link to read. Thank you~

yuji

Subject: the data type of ['x']

From: dpb

Date: 21 Apr, 2012 12:53:52

Message: 2 of 15

On 4/20/2012 10:20 PM, Yuji wrote:
> Hi there~
>
> I have a question about the fundamental data type issue.
>
>>> ['xx' 'yy' 1 2 'z']
>
> ans =
>
> xxyyz
>
> What is the data type?

Look and see...

 >> t=['xx' 'yy' 1 2 'z']
t =
xxyyz
 >> whos t
   Name Size Bytes Class

   t 1x7 14 char array

Grand total is 7 elements using 14 bytes

 >>
> What's the difference between ['xx'] and {'xx'} ?

 >> whos c
   Name Size Bytes Class

   c 1x1 96 cell array

Grand total is 3 elements using 96 bytes

 >>
>
> Is it illegal for a vector to have both char element and num element at
> the same time?

Yes, that's one of the reasons for cell arrays (arrays of varying types
in a container, basically).

>
> If somebody could explain this or give me a link to read. Thank you~

Read the "Getting Started" section in the documentation.

doc
doc cell

--

Subject: the data type of ['x']

From: Yuji

Date: 21 Apr, 2012 16:57:07

Message: 3 of 15

Thank you dpb~

And I found a=['xx' 'yy'] is not a straightforward way to store strings. because it's "digital by digital" based, namely a(1)=x rather than a(1)=xx.

So I'm thinking what's good about a=['xx' 'yy']?
Do people usually use a=['xx' 'yy'] as an alternative of "strcat" (string concatenation) and/or "sprintf", when no numbers are involved?

Thank you for your help~

yuji




dpb <none@non.net> wrote in message <jmuaku$9ne$1@speranza.aioe.org>...
> On 4/20/2012 10:20 PM, Yuji wrote:
> > Hi there~
> >
> > I have a question about the fundamental data type issue.
> >
> >>> ['xx' 'yy' 1 2 'z']
> >
> > ans =
> >
> > xxyyz
> >
> > What is the data type?
>
> Look and see...
>
> >> t=['xx' 'yy' 1 2 'z']
> t =
> xxyyz
> >> whos t
> Name Size Bytes Class
>
> t 1x7 14 char array
>
> Grand total is 7 elements using 14 bytes
>
> >>
> > What's the difference between ['xx'] and {'xx'} ?
>
> >> whos c
> Name Size Bytes Class
>
> c 1x1 96 cell array
>
> Grand total is 3 elements using 96 bytes
>
> >>
> >
> > Is it illegal for a vector to have both char element and num element at
> > the same time?
>
> Yes, that's one of the reasons for cell arrays (arrays of varying types
> in a container, basically).
>
> >
> > If somebody could explain this or give me a link to read. Thank you~
>
> Read the "Getting Started" section in the documentation.
>
> doc
> doc cell
>
> --

Subject: the data type of ['x']

From: ImageAnalyst

Date: 21 Apr, 2012 17:37:00

Message: 4 of 15

yuji: See my answer (#5) here in this thread for an explanation of
cell arrays: http://www.mathworks.com/matlabcentral/newsreader/view_thread/319074#873364

Subject: the data type of ['x']

From: ImageAnalyst

Date: 21 Apr, 2012 17:41:41

Message: 5 of 15

On Apr 21, 12:57 pm, "Yuji " <yuji.zh...@tufts.edu> wrote:
> Thank you dpb~
>
> And I found a=['xx' 'yy'] is not a straightforward way to store strings. because it's "digital by digital" based, namely a(1)=x rather than a(1)=xx.
>
> So I'm thinking what's good about  a=['xx' 'yy']?
> Do people usually use a=['xx' 'yy'] as an alternative of  "strcat" (string concatenation) and/or "sprintf", when no numbers are involved?
>
> Thank you for your help~
>
> yuji
-----------------------------------------------------------------------------
yuji:
Cell arrays are used when the strings don't have the same length, like
'a' and 'abc'. Character arrays are used when all your characters can
fit int a nice rectangular array like
a b c d
z y x v
m n s t

I usually use sprintf() to build up my strings since I'm used to the
"C" way of doing things and it allows me to easily mix strings and
numbers. You can do it with the brackets and apostrophes but I find
it more confusing to look at. I almost never use the string functions
like strvcat, horzcat, or whatever. Again (see my prior response),
see my discussion of cells here: http://www.mathworks.com/matlabcentral/newsreader/view_thread/319074#873364

Subject: the data type of ['x']

From: dpb

Date: 21 Apr, 2012 17:44:28

Message: 6 of 15

On 4/21/2012 11:57 AM, Yuji wrote:

...[top posting repaired...please don't do that: hard follow
conversation makes]...
> dpb <none@non.net> wrote in message <jmuaku$9ne$1@speranza.aioe.org>...
>> On 4/20/2012 10:20 PM, Yuji wrote:
...

>> > What's the difference between ['xx'] and {'xx'} ?
>>
>> >> whos c
>> Name Size Bytes Class
>>
>> c 1x1 96 cell array
>>
>> Grand total is 3 elements using 96 bytes
>>
...
> And I found a=['xx' 'yy'] is not a straightforward way to store
> strings. because it's "digital by digital" based, namely a(1)=x
> rather than a(1)=xx.

Well, rather than that description I'd recommend using the Matlab
definition which is a "character array".

 > So I'm thinking what's good about a=['xx' 'yy']?
 > Do people usually use a=['xx' 'yy'] as an alternative of "strcat"
 > (string concatenation) and/or "sprintf", when no numbers are involved?
 > Thank you for your help~
...

 From a historical perspective, Matlab didn't support cell arrays so the
character array was the only way to deal w/ character data. Although
that is an era long past now, it is part of the legacy and so they still
remain for no other reason than compatibility (not that that's the only
reason).

It depends greatly on the intent as to which form is most/more suitable
and w/ experience you'll undoubtedly find cases to prefer the one or the
other and learn to recognize when which is the better choice.

For pure string handling such as displaying strings as text, etc., the
convenience of the cell array which may hold a character array as a
single entity.

Recall the previous example--
 >> c={'xx'}; % a single cell array
 >> whos c
   Name Size Bytes Class
   c 1x1 96 cell array
Grand total is 3 elements using 96 bytes

 >> s=c{1}; % dereference the cell to a variable
 >> whos s
   Name Size Bytes Class
   s 1x2 4 char array
Grand total is 2 elements using 4 bytes

 >>

Note that the _contents_ of c{} are indeed a char array and note by the
by the storage difference in the overhead of the cell array to hold only
the actual four bytes (this can become an issue if were to create a very
large cell array of very small objects. NB--

 >> c{2}='yy'; % add another cell to c{}
 >> whos c
   Name Size Bytes Class
   c 1x2 192 cell array
Grand total is 6 elements using 192 bytes

 >>

The storage has also double; it's not a one-time overhead but on a
cell-by-cell basis.

OTOH, say one were working through a list of options and looking at the
result...

s='RGB';
for idx=1:length(s)
   switch s(idx)
     case {'R'}
       disp('RED')
     case {'G'}
       disp('GREEN')
     case {'B'}
       disp('BLUE')
   end
end

Somewhat contrived, but you get the idea; it's occasionally useful to
reference just a given letter by position in which case it's more
convenient if they're character arrays rather than cell strings.

So to repeat, "it all depends"....

--

Subject: the data type of ['x']

From: Steven_Lord

Date: 23 Apr, 2012 14:53:19

Message: 7 of 15



"Yuji " <yuji.zhang@tufts.edu> wrote in message
news:jmuot3$6sj$1@newscl01ah.mathworks.com...
> Thank you dpb~
>
> And I found a=['xx' 'yy'] is not a straightforward way to store strings.
> because it's "digital by digital" based, namely a(1)=x rather than
> a(1)=xx.

That's correct. The code you posted creates a vector of 4 characters and the
first character in that vector is indeed 'x'.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: the data type of ['x']

From: Yuji

Date: 24 Apr, 2012 17:19:14

Message: 8 of 15

I see. I think sprintf is clear too.

Thank you~



ImageAnalyst <imageanalyst@mailinator.com> wrote in message <405680d0-3bd2-491c-b555-a544373f573b@h9g2000yqe.googlegroups.com>...
> On Apr 21, 12:57 pm, "Yuji " <yuji.zh...@tufts.edu> wrote:
> > Thank you dpb~
> >
> > And I found a=['xx' 'yy'] is not a straightforward way to store strings. because it's "digital by digital" based, namely a(1)=x rather than a(1)=xx.
> >
> > So I'm thinking what's good about  a=['xx' 'yy']?
> > Do people usually use a=['xx' 'yy'] as an alternative of  "strcat" (string concatenation) and/or "sprintf", when no numbers are involved?
> >
> > Thank you for your help~
> >
> > yuji
> -----------------------------------------------------------------------------
> yuji:
> Cell arrays are used when the strings don't have the same length, like
> 'a' and 'abc'. Character arrays are used when all your characters can
> fit int a nice rectangular array like
> a b c d
> z y x v
> m n s t
>
> I usually use sprintf() to build up my strings since I'm used to the
> "C" way of doing things and it allows me to easily mix strings and
> numbers. You can do it with the brackets and apostrophes but I find
> it more confusing to look at. I almost never use the string functions
> like strvcat, horzcat, or whatever. Again (see my prior response),
> see my discussion of cells here: http://www.mathworks.com/matlabcentral/newsreader/view_thread/319074#873364

Subject: the data type of ['x']

From: Yuji

Date: 24 Apr, 2012 17:31:08

Message: 9 of 15

Thank you for the detailed explanation, dpb!

Still wanna check about two things with you.

1.
Quote - - - - -
 >> c={'xx'}; % a single cell array
 >> whos c
   Name Size Bytes Class
   c 1x1 96 cell array
Grand total is 3 elements using 96 bytes
Quote end - - - - -

Why it's 3 elements?

2.
Quote - - - - -
s='RGB';
for idx=1:length(s)
   switch s(idx)
     case {'R'}
       disp('RED')
     case {'G'}
       disp('GREEN')
     case {'B'}
       disp('BLUE')
   end
end
Quote end - - - - -

This code works. When i remove the {}, it still works.
s='RGB';
for idx=1:length(s)
   switch s(idx)
     case 'R'
       disp('RED')
     case 'G'
       disp('GREEN')
     case 'B'
       disp('BLUE')
   end
end

Why here Matlab treat 'R' and {'R'} the same thing?

Thank you~

yuji




dpb <none@non.net> wrote in message <jmurlr$j3a$1@speranza.aioe.org>...
> On 4/21/2012 11:57 AM, Yuji wrote:
>
> ...[top posting repaired...please don't do that: hard follow
> conversation makes]...
> > dpb <none@non.net> wrote in message <jmuaku$9ne$1@speranza.aioe.org>...
> >> On 4/20/2012 10:20 PM, Yuji wrote:
> ...
>
> >> > What's the difference between ['xx'] and {'xx'} ?
> >>
> >> >> whos c
> >> Name Size Bytes Class
> >>
> >> c 1x1 96 cell array
> >>
> >> Grand total is 3 elements using 96 bytes
> >>
> ...
> > And I found a=['xx' 'yy'] is not a straightforward way to store
> > strings. because it's "digital by digital" based, namely a(1)=x
> > rather than a(1)=xx.
>
> Well, rather than that description I'd recommend using the Matlab
> definition which is a "character array".
>
> > So I'm thinking what's good about a=['xx' 'yy']?
> > Do people usually use a=['xx' 'yy'] as an alternative of "strcat"
> > (string concatenation) and/or "sprintf", when no numbers are involved?
> > Thank you for your help~
> ...
>
> From a historical perspective, Matlab didn't support cell arrays so the
> character array was the only way to deal w/ character data. Although
> that is an era long past now, it is part of the legacy and so they still
> remain for no other reason than compatibility (not that that's the only
> reason).
>
> It depends greatly on the intent as to which form is most/more suitable
> and w/ experience you'll undoubtedly find cases to prefer the one or the
> other and learn to recognize when which is the better choice.
>
> For pure string handling such as displaying strings as text, etc., the
> convenience of the cell array which may hold a character array as a
> single entity.
>
> Recall the previous example--
> >> c={'xx'}; % a single cell array
> >> whos c
> Name Size Bytes Class
> c 1x1 96 cell array
> Grand total is 3 elements using 96 bytes
>
> >> s=c{1}; % dereference the cell to a variable
> >> whos s
> Name Size Bytes Class
> s 1x2 4 char array
> Grand total is 2 elements using 4 bytes
>
> >>
>
> Note that the _contents_ of c{} are indeed a char array and note by the
> by the storage difference in the overhead of the cell array to hold only
> the actual four bytes (this can become an issue if were to create a very
> large cell array of very small objects. NB--
>
> >> c{2}='yy'; % add another cell to c{}
> >> whos c
> Name Size Bytes Class
> c 1x2 192 cell array
> Grand total is 6 elements using 192 bytes
>
> >>
>
> The storage has also double; it's not a one-time overhead but on a
> cell-by-cell basis.
>
> OTOH, say one were working through a list of options and looking at the
> result...
>
> s='RGB';
> for idx=1:length(s)
> switch s(idx)
> case {'R'}
> disp('RED')
> case {'G'}
> disp('GREEN')
> case {'B'}
> disp('BLUE')
> end
> end
>
> Somewhat contrived, but you get the idea; it's occasionally useful to
> reference just a given letter by position in which case it's more
> convenient if they're character arrays rather than cell strings.
>
> So to repeat, "it all depends"....
>
> --

Subject: the data type of ['x']

From: Yuji

Date: 24 Apr, 2012 17:33:13

Message: 10 of 15

Thanks for your help Steven~

yuji



"Steven_Lord" <slord@mathworks.com> wrote in message <jn3qcv$ivn$1@newscl01ah.mathworks.com>...
>
>
> "Yuji " <yuji.zhang@tufts.edu> wrote in message
> news:jmuot3$6sj$1@newscl01ah.mathworks.com...
> > Thank you dpb~
> >
> > And I found a=['xx' 'yy'] is not a straightforward way to store strings.
> > because it's "digital by digital" based, namely a(1)=x rather than
> > a(1)=xx.
>
> That's correct. The code you posted creates a vector of 4 characters and the
> first character in that vector is indeed 'x'.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: the data type of ['x']

From: dpb

Date: 24 Apr, 2012 17:53:02

Message: 11 of 15

On 4/24/2012 12:31 PM, Yuji wrote:
> Thank you for the detailed explanation, dpb!
>
> Still wanna check about two things with you.
> 1.
> Quote - - - - - >> c={'xx'}; % a single cell array
> >> whos c
> Name Size Bytes Class
> c 1x1 96 cell array
> Grand total is 3 elements using 96 bytes
> Quote end - - - - -
> Why it's 3 elements?

One for the cell, two for the contents. To see this try
c={'xxx'};
whos c

> 2.
> Quote - - - - - s='RGB';
> for idx=1:length(s)
> switch s(idx)
> case {'R'}
> disp('RED')
...

> Quote end - - - - -
> This code works. When i remove the {}, it still works.
> s='RGB';
> for idx=1:length(s)
> switch s(idx)
> case 'R'
> disp('RED')
...

> Why here Matlab treat 'R' and {'R'} the same thing?
...

'Cuz they are the same thing as literals. I use the {} as a habit
consider the following snippet (from doc on SWITCH)

method='linear';

switch lower(method)
     case {'linear','bilinear'}, disp('Method is linear')
     case 'cubic', disp('Method is cubic')
     case 'nearest', disp('Method is nearest')
     otherwise, disp('Unknown method.')
end
Method is linear

Now try it removing the {} around the CASE conditions and see what happens.
 >> switch lower(method)
     case 'linear','bilinear', disp('Method is linear')
     case 'cubic', disp('Method is cubic')
     case 'nearest', disp('Method is nearest')
     otherwise, disp('Unknown method.')
end
ans =
bilinear
Method is linear
 >>

Can you see where the 'bilinear' output came from?

The other thing that's different is consider the above except let's
define the possible methods as

methods=strvcat('linear','bilinear','cubic','nearest','bicubic')

and we tried something like

for idx=1:size(methods,1)
   method=methods(idx);
   %insert above SWITCH construct here
   ...
end

Try that. What mod need to make to get that to work?

On the other hand, what if we used

methods={'linear';'bilinear';'cubic';'nearest';'bicubic'};

instead?

--

Subject: the data type of ['x']

From: Steven_Lord

Date: 24 Apr, 2012 18:55:14

Message: 12 of 15



"dpb" <none@non.net> wrote in message news:jn6p9u$gbl$1@speranza.aioe.org...
> On 4/24/2012 12:31 PM, Yuji wrote:

*snip*

>> 2.
>> Quote - - - - - s='RGB';
>> for idx=1:length(s)
>> switch s(idx)
>> case {'R'}
>> disp('RED')
> ...
>
>> Quote end - - - - -
>> This code works. When i remove the {}, it still works.
>> s='RGB';
>> for idx=1:length(s)
>> switch s(idx)
>> case 'R'
>> disp('RED')
> ...
>
>> Why here Matlab treat 'R' and {'R'} the same thing?
> ...
>
> 'Cuz they are the same thing as literals.

They are not. In many cases they're interchangeable, but that's not the
same.

The reason they both work as CASE expressions for the SWITCH expression 'R'
is because SWITCH and CASE know how to deal with both regular char arrays
and cell arrays whose elements contain char arrays [sometimes called a
CELLSTR.]

http://www.mathworks.com/help/techdoc/ref/switch.html

"A case is true when: ... For a cell array case_expression, at least one of
the elements of the cell array matches switch_expression, as defined above
for numbers, strings, and objects."

http://www.mathworks.com/help/techdoc/ref/cellstr.html

In the situation (aren't you glad I didn't say case? ;) where the CASE
expression is the cell array {'R'}, the SWITCH expression 'R' matches
because strcmp('R', 'R') [using the first/only element of the cell array
{'R'} as the first 'R'] is true.
When the CASE expression is the char array 'R', strcmp('R', 'R') is true and
so that's the case that is executed.

> I use the {} as a habit consider the following snippet (from doc on
> SWITCH)
>
> method='linear';
>
> switch lower(method)
> case {'linear','bilinear'}, disp('Method is linear')
> case 'cubic', disp('Method is cubic')
> case 'nearest', disp('Method is nearest')
> otherwise, disp('Unknown method.')
> end
> Method is linear
>
> Now try it removing the {} around the CASE conditions and see what
> happens.
> >> switch lower(method)
> case 'linear','bilinear', disp('Method is linear')
> case 'cubic', disp('Method is cubic')
> case 'nearest', disp('Method is nearest')
> otherwise, disp('Unknown method.')
> end
> ans =
> bilinear
> Method is linear
> >>
>
> Can you see where the 'bilinear' output came from?
>
> The other thing that's different is consider the above except let's define
> the possible methods as
>
> methods=strvcat('linear','bilinear','cubic','nearest','bicubic')
>
> and we tried something like
>
> for idx=1:size(methods,1)
> method=methods(idx);
> %insert above SWITCH construct here
> ...
> end
>
> Try that. What mod need to make to get that to work?

Don't forget the DEBLANK.

> On the other hand, what if we used
>
> methods={'linear';'bilinear';'cubic';'nearest';'bicubic'};
>
> instead?

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: the data type of ['x']

From: Yuji

Date: 25 Apr, 2012 22:21:12

Message: 13 of 15

Thank you so much, dpb~~

1. Still don't see "3"

>> c={'xxx'}; whos c
  Name Size Bytes Class Attributes

  c 1x1 66 cell

It doesn't say anything about "3"...


2.

case {'linear','bilinear'}, disp('Method is linear') is nice! Nice example why {} is useful.


3.

>> methods=strvcat('linear','bilinear','cubic','nearest','bicubic')

methods =

linear
bilinear
cubic
nearest
bicubic

>> for idx=1:size(methods,1)
   method=methods(idx);
   disp(method);
   %insert above SWITCH construct here
   ...
end
l
b
c
n
b

So methods(idx) only gets the first letter in each line.

I see. I remember I read in the help, in the above case, methods in a matrix with blanks to make it a rectangular matrix.

>> size(methods,1)

ans =

     5

>> size(methods,2)

ans =

     8

So it's 5 lines, 8 columns. Because the longest word in each line is 8 letters.

To refer to each word, method(line_number) doesn't work. We need method(line_number,:)

>> methods(2)

ans =

b

>> methods(2,:)

ans =

bilinear


A easier way (to me) to deal with that may be this:

>>methods={'linear','bilinear','cubic','nearest','bicubic'}

methods =

    'linear' 'bilinear' 'cubic' 'nearest' 'bicubic'

>> methods{1}

ans =

linear



It's so nice to discuss with you. Thank you again~

btw, I'm now dying doing Fourier transform things. Are you also familiar with this? I can post a message and email you to tell you, if you're interested.




dpb <none@non.net> wrote in message <jn6p9u$gbl$1@speranza.aioe.org>...
> On 4/24/2012 12:31 PM, Yuji wrote:
> > Thank you for the detailed explanation, dpb!
> >
> > Still wanna check about two things with you.
> > 1.
> > Quote - - - - - >> c={'xx'}; % a single cell array
> > >> whos c
> > Name Size Bytes Class
> > c 1x1 96 cell array
> > Grand total is 3 elements using 96 bytes
> > Quote end - - - - -
> > Why it's 3 elements?
>
> One for the cell, two for the contents. To see this try
> c={'xxx'};
> whos c
>
> > 2.
> > Quote - - - - - s='RGB';
> > for idx=1:length(s)
> > switch s(idx)
> > case {'R'}
> > disp('RED')
> ...
>
> > Quote end - - - - -
> > This code works. When i remove the {}, it still works.
> > s='RGB';
> > for idx=1:length(s)
> > switch s(idx)
> > case 'R'
> > disp('RED')
> ...
>
> > Why here Matlab treat 'R' and {'R'} the same thing?
> ...
>
> 'Cuz they are the same thing as literals. I use the {} as a habit
> consider the following snippet (from doc on SWITCH)
>
> method='linear';
>
> switch lower(method)
> case {'linear','bilinear'}, disp('Method is linear')
> case 'cubic', disp('Method is cubic')
> case 'nearest', disp('Method is nearest')
> otherwise, disp('Unknown method.')
> end
> Method is linear
>
> Now try it removing the {} around the CASE conditions and see what happens.
> >> switch lower(method)
> case 'linear','bilinear', disp('Method is linear')
> case 'cubic', disp('Method is cubic')
> case 'nearest', disp('Method is nearest')
> otherwise, disp('Unknown method.')
> end
> ans =
> bilinear
> Method is linear
> >>
>
> Can you see where the 'bilinear' output came from?
>
> The other thing that's different is consider the above except let's
> define the possible methods as
>
> methods=strvcat('linear','bilinear','cubic','nearest','bicubic')
>
> and we tried something like
>
> for idx=1:size(methods,1)
> method=methods(idx);
> %insert above SWITCH construct here
> ...
> end
>
> Try that. What mod need to make to get that to work?
>
> On the other hand, what if we used
>
> methods={'linear';'bilinear';'cubic';'nearest';'bicubic'};
>
> instead?
>
> --

Subject: the data type of ['x']

From: dpb

Date: 25 Apr, 2012 22:52:54

Message: 14 of 15

On 4/25/2012 5:21 PM, Yuji wrote:
> Thank you so much, dpb~~
>
> 1. Still don't see "3"
>
>>> c={'xxx'}; whos c
> Name Size Bytes Class Attributes
>
> c 1x1 66 cell
> It doesn't say anything about "3"...
>
...

You don't get the "grand totals" line in your release????

 >> c={'xx'};
 >> whos c
   Name Size Bytes Class

   c 1x1 96 cell array

Grand total is 3 elements using 96 bytes

 >> >> c={'xxx'};
 >> whos c
   Name Size Bytes Class

   c 1x1 98 cell array

Grand total is 4 elements using 98 bytes

 >>

NB the total is one element over the length of the string...

--

Subject: the data type of ['x']

From: Steven_Lord

Date: 26 Apr, 2012 14:17:31

Message: 15 of 15



"dpb" <none@non.net> wrote in message news:jn9v89$ee2$1@speranza.aioe.org...
> On 4/25/2012 5:21 PM, Yuji wrote:
>> Thank you so much, dpb~~
>>
>> 1. Still don't see "3"
>>
>>>> c={'xxx'}; whos c
>> Name Size Bytes Class Attributes
>>
>> c 1x1 66 cell
>> It doesn't say anything about "3"...
>>
> ...
>
> You don't get the "grand totals" line in your release????

That line was removed in release R2006b.

http://www.mathworks.com/help/techdoc/rn/bqt6wls.html#bqu7k3m-1

I'm not certain of the exact reason, but if I had to guess it's because it's
hard to decide or describe what should be counted in the "grand total". For
instance (using release R14, since I had it open)


>> C = {repmat('a', 1, 10000)};
>> whos
  Name Size Bytes Class

  C 1x1 20060 cell array

Grand total is 10001 elements using 20060 bytes

all the elements inside a cell array element count (as does the cell array
element itself), but:


>> z = ones(1000);
>> fh = @(x) x+z;
>> clear z
>> whos
  Name Size Bytes Class

  fh 1x1 16 function_handle array

Grand total is 1 element using 16 bytes

>> F = functions(fh);
>> F.workspace
ans =
    z: [1000x1000 double]

elements of variables in an anonymous function workspace did not.


"copy on write" behavior clouds matters even more. Before executing Loren's
last example in this blog posting:

http://blogs.mathworks.com/loren/2006/05/10/memory-management-for-functions-and-variables/

can you tell me how many elements WHOS will show? Should it show 27 (since
s.B and sNew.B share memory) or 36 (since each field of s and sNew contain 9
elements, regardless of whether they share memory?) Actually it shows 40
elements, 36 plus one extra for each field in s and sNew.


>> s.A = rand(3);
>> s.B = magic(3);
>> sNew = s;
>> sNew.A(3) = 14;
>> whos
  Name Size Bytes Class

  s 1x1 392 struct array
  sNew 1x1 392 struct array

Grand total is 40 elements using 784 bytes


For that same reason, the bytes line was also misleading (since s.B and
sNew.B shared data, that figure's high by at least 9 (elements)*8
(bytes/element) = 72 bytes.)

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us