How do I assign a full word to a single array position?

I have an user created array of real integers, I am trying to have a resulting array that shows each position as "odd" or "even". My issue is that each character of a word has its own position in an array. How do you make matlab accept an entire word as just one character?

 Accepted Answer

You can't. For matlab, each character is the same as single number and as you know you can only have one number per matrix element.
You can however store a whole matrix or char in a single cell of a cell array:
>>c = {'a string', 2; [1 2; 3 4], 'another string'};
>>c{1,1}
ans =
'a string'
Or, since r2016b, you can have an array of string objects. Such an array can only contain strings however:
>>s = string({'a string', 'another string'; 'yet another', 'final one'})
s =
2×2 string array
"a string" "another string"
"yet another" "final one"

More Answers (1)

As of R2016b, you can use strings() to create a 2 dimension string array, which can then store 'odd' or 'even' at each position.
For any previous version, you need to use a cell array of strings.

Categories

Products

Asked:

on 19 Oct 2016

Answered:

on 19 Oct 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!