Search code examples
stringoctave

Accessing strings from an array of strings in GNU Octave


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

Solution

  • Use cell arrays instead.

    octave:1> s = { 'hello'; 'ahoy' };
    
    octave:2> s{1}
    ans = hello
    
    octave:3> s{2}
    ans = ahoy
    

    See https://octave.org/doc/v5.2.0/Cell-Arrays.html#Cell-Arrays