简体   繁体   English

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

[英]Accessing strings from a array of strings in GNU Octave

How to access the whole elements hello and ahoy in Octave?如何在 Octave 中访问整个元素helloahoy 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

Use cell arrays instead.请改用单元格 arrays。

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参见https://octave.org/doc/v5.2.0/Cell-Arrays.html#Cell-Arrays

Check the size and type of s to understand what's going on:检查 s 的大小和类型以了解发生了什么:

octave:5> size(s)
ans =

   2   5

octave:6> class(s)
ans = char

It's a 2x5 matrix of characters.它是一个 2x5 的字符矩阵。 To index, use matrix indexing.要索引,请使用矩阵索引。 For example getting the first row:例如获取第一行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM