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

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

如何在Octave中访问整个元素

hello
ahoy
?仅打印每个字符串中的第一个字符。

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

hello
ahoy 

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

改用元胞数组。

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


4
投票

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

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.