Accessing strings from a array of strings in GNU Octave

Viewed 3641

How to access the whole elements hello and ahoy in Octave? Only the first character in each string is being printed.

octave:1> s = ["hello";"ahoy"]
s =

hello
ahoy 

octave:2> s(1)
ans = h
octave:3> s(2)
ans = a
2 Answers

Check the size and type of s to understand what's going on:

octave:5> size(s)
ans =

   2   5

octave:6> class(s)
ans = char

It's a 2x5 matrix of characters. To index, use matrix indexing. For example getting the first row:

octave:7> s(1,:)
ans = hello
Related