从GNU Octave中的字符串数组访问字符串

问题描述 投票:0回答:2

如何在八度中访问整个元素helloahoy?每个字符串中只打印第一个字符。

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

hello
ahoy 

octave:2> s(1)
ans = h
octave:3> s(2)
ans = a
string octave
2个回答
0
投票

改为使用单元格数组。

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

octave:2> s{1}
ans = hello

octave:3> s{2}
ans = ahoy

请参见https://octave.org/doc/v5.2.0/Cell-Arrays.html#Cell-Arrays


0
投票

检查的大小和类型以了解发生了什么:

octave:5> size(s)
ans =

   2   5

octave:6> class(s)
ans = char

这是2x5的字符矩阵。要进行索引,请使用矩阵索引。例如,获取第一行:

octave:7> s(1,:)
ans = hello
© www.soinside.com 2019 - 2024. All rights reserved.