a string, a string array and character array

a string, a string array and character array

Post by Arthur Zhe » Fri, 12 Dec 2008 01:50:19


Hi,
What is the difference between "a string", "a string array" and "character array" in Matlab? Can anyone show me the difference by an example? thanks!

Hao Zheng (Arthur)
ECE, Georgia Tech
 
 
 

a string, a string array and character array

Post by Rune Allno » Fri, 12 Dec 2008 02:13:23


A "char array" is an array that contains numerical data of
type "char", that is numbers in the range -128 - 127.
A "string" is an array that contains characters (letters)
instead of numbers. Certain operations work differently
with strings, like sorting. With strings, lowercase and
uppercase letters have different numerical values but the
same ordinal value (that is, "Abc" and "abc" are equivalent).

In certain programming languages opereations like "+" work
a bit differently, as with char arrays a+b will give the
numerical values stored in array a added to the numerical
values stored in array b, while for strings "abc"+"def"
results in the string "abcdef".

Rune

 
 
 

a string, a string array and character array

Post by Arthur Zhe » Fri, 12 Dec 2008 02:36:01


---------------------------------------------------------------------------------------------
I am still confused... First of all, what is the difference between "a string" and "a string array"?
 
 
 

a string, a string array and character array

Post by Walter Rob » Fri, 12 Dec 2008 02:49:44


I would have to dig for a while to find a computer language
which defined its "char" to be in the range -128 to 127.
For example, C doesn't: "char" has an implementation-defined
range that can be signed or unsigned; "char" in C has a certain
minimum span of values, but there is no maximum span of values
defined in C, and there -are- C implementations in which 'char'
is a 32 bit quantity (e.g., Cray, and some DSPs.)

Possibly one of the BASIC defined in terms of -128 to 127.
Defining char as unsigned is more common in computer languages.

In Matlab, as of somewhere around version 7.0 (I'd have to research
the exact version), individual char are stored as unsigned values from
0 to 65535.


ans =

65535

Warning: Out of range or non-integer values truncated during conversion to character.

ans =

65535



Urrr, I've never encountered that definition of "string" before.


In Matlab, a "string" is a common phrasing for a row vector of char.
A character array is an array of character type; if the array happens
to be two dimensional and the first dimension happens to be 1, then
the character array could also be called a "string". Two dimensional
arrays of characters are common in Matlab; the older convention in Matlab is
that if you need to pass in a list of strings, that you put them all
together in a two-dimensional array of characters whose width is the
width of the longest of the individual strings and with all of the shorter
lines right-padded with blanks.

Matlab also refers to "cell strings"; see the documentation
for cell2str().

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?
 
 
 

a string, a string array and character array

Post by Rune Allno » Fri, 12 Dec 2008 03:05:49


The OP is clearly a newbie. I think it is premature to
start splitting hairs at this level.



I suppose you can read? That you are familiar with the
lexical sorting of words? And that you have some at least
peripherical knowledge of the ASCII (or ANSI) character
sets?

As for programming languages, the above is how strings
work in C++.

Rune
 
 
 

a string, a string array and character array

Post by Arthur Zhe » Fri, 12 Dec 2008 04:25:04

Let's talking within the range of matlab. What is the difference between "a string" and "a string array"?
 
 
 

a string, a string array and character array

Post by Walter Rob » Fri, 12 Dec 2008 05:12:15

une Allnor wrote:


I disagree. You gave an authoritative-sounding answer that was
wrong for Matlab (which does not have negative character values)
and was wrong for the C++ language that you were importing the semantics of:

According to ISO/IEC 14882:1998(E) final draft:

http://www.kuzbass.ru:8086/docs/isocpp/basic.html#basic.fundamental
-1- Objects declared as characters (char) shall be large enough to store
any member of the implementation's basic character set. If a character from
this set is stored in a character object, the integral value of that
character object is equal to the value of the single character literal form
of that character. It is implementation-defined whether a char object can
hold negative values.






"Abc" and "abc" are, in C++, string literals [lex.string],
http://www.kuzbass.ru:8086/docs/isocpp/lex.html#lex.string
and string literals do NOT behave in the way you described.
Members of class string [lib.strings]
http://www.kuzbass.ru:8086/docs/isocpp/lib-strings.html
do not behave as you describe either: whether 'A' and 'a'
are treated the same depends upon the traits::eq() method
or the traits::compare() method (depending upon the exact
operation involved). Perhaps I am just not looking in the correct place,
but I see no evidence in [lib.strings] that -requires- "Abc" to be
treated as equivalent to "abc" for any purpose. I imagine it would
depend upon the Locale in effect -- and since you reference
ASCII and ANSI, you are implicitly referencing the "C" locale, in
which 'A' and 'a' would NOT compare as equal.

Note that [lib.strings] does not define any sort method and the text
of that section does not even use the word "sort". The compare() methods
of that section all refer back to traits.

Lexical sorting of strings is a messy topic, and the "right" way to do lexical
sorts is context-dependent, not something that can be encoded in a per-character
table. For example there is continued controversy in
library science over how to sort titles that start with the article "The",
and in practice it can come down to the role that the article is playing
in the title: e.g., "The Winning Strategy" could sort differently depending
on the emphasis that the author was placing on the fact that the winning
strategy being described was the *only* winning strategy. Compare the
theological implications of sorting as "Holy Bible, The"
against "The Holy Bible".
 
 
 

a string, a string array and character array

Post by Walter Rob » Fri, 12 Dec 2008 05:30:11


"string array" is used, as best I can see, only in discussions of Mex routines,
where it is "an mxArray having the mxCHAR_CLASS class", which is the same thing
as a "character array", the term used in the rest of Matlab.

When "string" is used in Matlab, it refers to a row array of character, which
is to say an array of type character in which the first dimension is 1, the
second dimension is 1 or more, and all remaining dimensions are 1.

Otherwise, Matlab refers to character arrays, in which some additional dimension
might be something other than 1.

You might be confused by references to a "cell string array" or a "cell array
of strings". The "cell" is key there and "string array" cannot be removed from
the phrase without distorting the meaning severely. A cell string array
or a cell array of strings is a cell array, the entries of which are strings
(that is, row array of char.)

{'Hi', 'There'}

is a cell string array: that is, it is a cell array, the first cell of which
contains the 1 x 2 character array 'Hi', and the second cell of which contains
the 1 x 5 character array 'There'. A cell string array is an instance of a
generalized cell array; for example, {'Hi' 173 'There'} is a valid cell array,
the middle cell of which contains the 1 x 1 double array with the value 173,
and a cell string array is a cell array in which all the entries happen to be
strings.

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?
 
 
 

a string, a string array and character array

Post by Arthur Zhe » Fri, 12 Dec 2008 05:53:02


----------------------------------------------------------------------
What is "3D" in your command? Do you mean every string element of the string array must be of the same length?
 
 
 

a string, a string array and character array

Post by Arthur Zhe » Fri, 12 Dec 2008 06:01:04

Hi,
"String array" is not restricted to Mex routines. If you type "doc knnclassify", you can see "GROUP can be a numeric vector, a string array, or a cell array of strings."
 
 
 

a string, a string array and character array

Post by someon » Fri, 12 Dec 2008 06:38:03


It seems some newsgroup readers turn "= " into "=3D" for some reason.
You can see this behavior on several other threads of this newsgroup.
 
 
 

a string, a string array and character array

Post by Arthur Zhe » Fri, 12 Dec 2008 07:02:03

In my understand, can I summarize as the following example:

1. This is a string: str='This is a string', which is also a character array, right?

2. This is a cell array of strings: strcell = {'This', 'is', 'Cellarray', 'of strings'};

3. This is a string array: stra = str2mat('Hi','World'); We must use str2mat function to do the padding in order to may the length of 'Hi' and 'world' the same.

Please comment on my examples. Thank you all.
 
 
 

a string, a string array and character array

Post by NZTideMa » Fri, 12 Dec 2008 07:33:07


You've got it, buster!!
 
 
 

a string, a string array and character array

Post by Arthur Zhe » Fri, 12 Dec 2008 07:43:02


Thank you, NZTideMan