Indexing with "string" values in Matlab

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Indexing with "string" values in Matlab

Rik-4
10/25/13

Could someone run the following test in Matlab and see what happens?

x = 1:100;
x("A")
x("ADBC")
y("ADBC") = "1234"

I'm hoping there are some errors instead of just lamely accepting strings
as an index into an array.

Cheers,
Rik
Reply | Threaded
Open this post in threaded view
|

Re: Indexing with "string" values in Matlab

Michael Godfrey
On 10/25/2013 12:03 PM, Rik wrote:
> x = 1:100;
> x("A")
> x("ADBC")
> y("ADBC") = "1234"
 >> x = 1:100;
 >> x("A")
??? x("A")
       |
Error: The input character is not valid in MATLAB statements or expressions.

 >> x("ADBC")
??? x("ADBC")
       |
Error: The input character is not valid in MATLAB statements or expressions.

 >> y("ADBC") = "1234"
??? y("ADBC") = "1234"
       |
Error: The input character is not valid in MATLAB statements or expressions.


Reply | Threaded
Open this post in threaded view
|

Re: Indexing with "string" values in Matlab

Richard Crozier
In reply to this post by Rik-4
On 25/10/2013 17:03, Rik wrote:

> 10/25/13
>
> Could someone run the following test in Matlab and see what happens?
>
> x = 1:100;
> x("A")
> x("ADBC")
> y("ADBC") = "1234"
>
> I'm hoping there are some errors instead of just lamely accepting strings
> as an index into an array.
>
> Cheers,
> Rik
>


 >> x = 1:100;
x('A')
x('ADBC')
y('ADBC') = '1234'

ans =

     65.0000e+000


ans =

     65.0000e+000    68.0000e+000    66.0000e+000    67.0000e+000


y =

                                                                 1342


Regards,
Richard

--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.

Reply | Threaded
Open this post in threaded view
|

Re: Indexing with "string" values in Matlab

Michael Godfrey
In reply to this post by Rik-4
On 10/25/2013 12:03 PM, Rik wrote:
> x = 1:100;
> x("A")
> x("ADBC")
> y("ADBC") = "1234"
But, I spoke too soon.  With single quotes:
 >> x('A')

ans =

     65

 >> x('ABCD')

ans =

     65    66    67    68

 >> y('ADBC') = '1234'

y =

                                                                 1342

 >>


Reply | Threaded
Open this post in threaded view
|

Re: Indexing with "string" values in Matlab

John W. Eaton
Administrator
On 10/25/2013 12:11 PM, Michael D. Godfrey wrote:

>  >> y('ADBC') = '1234'
>
> y =
>
> 1342

Though it doesn't show in the quoted message above (stupid mailer) in
the original message it looked like the string was padded with blanks.
Is that correct?  What does

   clear y
   y('adbc') = '1234';
   abs (y)

show?  Does it fill with blanks (ASCII 32)?  Octave fills with 0 and
then doesn't display the NUL character.

jwe


Reply | Threaded
Open this post in threaded view
|

Re: Indexing with "string" values in Matlab

Richard Crozier
On 25/10/2013 17:33, John W. Eaton wrote:

> On 10/25/2013 12:11 PM, Michael D. Godfrey wrote:
>
>>  >> y('ADBC') = '1234'
>>
>> y =
>>
>> 1342
>
> Though it doesn't show in the quoted message above (stupid mailer) in
> the original message it looked like the string was padded with blanks.
> Is that correct?  What does
>
>    clear y
>    y('adbc') = '1234';
>    abs (y)
>
> show?  Does it fill with blanks (ASCII 32)?  Octave fills with 0 and
> then doesn't display the NUL character.
>
> jwe
>
>
>

from before:

 >> whos
   Name      Size             Bytes  Class     Attributes

   ans       1x4                 32  double
   x         1x100              800  double
   y         1x68               136  char

 >> fprintf(1, '%d', y)
00000000000000000000000000000000000000000000000000000000000000004951525

 >> clear y
   y('adbc') = '1234';
   abs (y)

ans =

   Columns 1 through 7

      0.0000e+000     0.0000e+000     0.0000e+000     0.0000e+000
0.0000e+000     0.0000e+000     0.0000e+000

   Columns 8 through 14

      0.0000e+000     0.0000e+000     0.0000e+000     0.0000e+000
0.0000e+000     0.0000e+000     0.0000e+000

   Columns 15 through 21

      0.0000e+000     0.0000e+000     0.0000e+000     0.0000e+000
0.0000e+000     0.0000e+000     0.0000e+000

   Columns 22 through 28

      0.0000e+000     0.0000e+000     0.0000e+000     0.0000e+000
0.0000e+000     0.0000e+000     0.0000e+000

   Columns 29 through 35

      0.0000e+000     0.0000e+000     0.0000e+000     0.0000e+000
0.0000e+000     0.0000e+000     0.0000e+000

   Columns 36 through 42

      0.0000e+000     0.0000e+000     0.0000e+000     0.0000e+000
0.0000e+000     0.0000e+000     0.0000e+000

   Columns 43 through 49

      0.0000e+000     0.0000e+000     0.0000e+000     0.0000e+000
0.0000e+000     0.0000e+000     0.0000e+000

   Columns 50 through 56

      0.0000e+000     0.0000e+000     0.0000e+000     0.0000e+000
0.0000e+000     0.0000e+000     0.0000e+000

   Columns 57 through 63

      0.0000e+000     0.0000e+000     0.0000e+000     0.0000e+000
0.0000e+000     0.0000e+000     0.0000e+000

   Columns 64 through 70

      0.0000e+000     0.0000e+000     0.0000e+000     0.0000e+000
0.0000e+000     0.0000e+000     0.0000e+000

   Columns 71 through 77

      0.0000e+000     0.0000e+000     0.0000e+000     0.0000e+000
0.0000e+000     0.0000e+000     0.0000e+000

   Columns 78 through 84

      0.0000e+000     0.0000e+000     0.0000e+000     0.0000e+000
0.0000e+000     0.0000e+000     0.0000e+000

   Columns 85 through 91

      0.0000e+000     0.0000e+000     0.0000e+000     0.0000e+000
0.0000e+000     0.0000e+000     0.0000e+000

   Columns 92 through 98

      0.0000e+000     0.0000e+000     0.0000e+000     0.0000e+000
0.0000e+000    49.0000e+000    51.0000e+000

   Columns 99 through 100

     52.0000e+000    50.0000e+000


Richard

--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.

Reply | Threaded
Open this post in threaded view
|

Re: Indexing with "string" values in Matlab

Michael Godfrey
In reply to this post by John W. Eaton
On 10/25/2013 12:33 PM, John W. Eaton wrote:
> clear y
>   y('adbc') = '1234';
>   abs (y)
>
> show?  Does it fill with blanks (ASCII 32)?  Octave fills with 0 and
> then doesn't display the NUL character.
>
> jwe
Yes, blanks got eaten up:
 >> abs(y)

ans =

   Columns 1 through 22

      0     0     0     0     0     0     0     0     0     0 0    
0     0     0     0     0     0     0     0     0     0     0

   Columns 23 through 44

      0     0     0     0     0     0     0     0     0     0 0    
0     0     0     0     0     0     0     0     0     0     0

   Columns 45 through 66

      0     0     0     0     0     0     0     0     0     0 0    
0     0     0     0     0     0     0     0     0     0     0

   Columns 67 through 88

      0     0     0     0     0     0     0     0     0     0 0    
0     0     0     0     0     0     0     0     0     0     0

   Columns 89 through 100

      0     0     0     0     0     0     0     0    49    51 52    50

 >
Reply | Threaded
Open this post in threaded view
|

Re: Indexing with "string" values in Matlab

John W. Eaton
Administrator
In reply to this post by Richard Crozier
On 10/25/2013 12:47 PM, Richard Crozier wrote:

> Name Size Bytes Class Attributes
>
> ans 1x4 32 double
> x 1x100 800 double
> y 1x68 136 char
>
>  >> fprintf(1, '%d', y)
> 00000000000000000000000000000000000000000000000000000000000000004951525
>
>  >> clear y
> y('adbc') = '1234';
> abs (y)
>
> ans =
>
> Columns 1 through 7
>
> 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000
> 0.0000e+000

Interesting, so it is displaying NUL as a blank?  Does that happen for
any other character values less than 32?  How is

   char (0:32)

displayed?

jwe

Reply | Threaded
Open this post in threaded view
|

Re: Indexing with "string" values in Matlab

Richard Crozier
On 25/10/2013 17:56, John W. Eaton wrote:
>
> Interesting, so it is displaying NUL as a blank?  Does that happen for
> any other character values less than 32?  How is
>
>    char (0:32)
>
> displayed?
>
> jwe


uhh, well, it isn't displayed in email very well but I get:

 >> char (0:32)

ans =

  



This is actually displayed as in the attached output

Richard

--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.


ml_out.PNG (8K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Indexing with "string" values in Matlab

John W. Eaton
Administrator
On 10/25/2013 01:15 PM, Richard Crozier wrote:

> On 25/10/2013 17:56, John W. Eaton wrote:
>>
>> Interesting, so it is displaying NUL as a blank? Does that happen for
>> any other character values less than 32? How is
>>
>> char (0:32)
>>
>> displayed?
>>
>> jwe
>
>
> uhh, well, it isn't displayed in email very well but I get:
>
>  >> char (0:32)
>
> ans =
>
> 
>
> 
>
> This is actually displayed as in the attached output

OK, so something is displayed, not just blanks.

So what should we do about display of ASCII NUL?  Leave it as it is now,
or opt for the Matlab compatible behavior of "display SPC instead"?

jwe