简体   繁体   English

Octave - 访问八度值列表中的值

[英]Octave - Accessing Values in octave_value_list

I am working within a C++ development environment while using the Octave C++ API.我在使用 Octave C++ API 的同时在 C++ 开发环境中工作。

I would like to know how to access single values in a returned octave_value_list when calling functions.我想知道如何在调用函数时访问返回的八度值列表中的单个值。

For example when I am calling the clock-function im getting returned an octave_value_list which shows the following:例如,当我调用时钟函数时,我返回了一个 Octave_value_list,它显示了以下内容:

2014.0000 8.0000 22.0000 10.0000 1.0000 44.7120 2014.0000 8.0000 22.0000 10.0000 1.0000 44.7120

I now would like to access each single value but Im somehow only able to get the year.我现在想访问每个值,但我不知何故只能获得年份。 I am missing some syntax, maybe someone can help me.我缺少一些语法,也许有人可以帮助我。

Useful documentation to answer your question:有用的文档来回答您的问题:


The values you mention are a multiple octave_value s in an octave_value_list , or is it a an array of values in a single octave_value in an octave_value_list ?你提到的值是一个多octave_value以S octave_value_list ,或者是一个值的数组中的单个octave_valueoctave_value_list

Note that a single octave_value is not necessarily a scalar value.请注意,单个octave_value度值不一定是标量值。 It can be a an array with n dimensions, and multiple elements.它可以是一个具有 n 维和多个元素的数组。 It can be a struct, a struct array, or a cell array.它可以是结构体、结构体数组或元胞数组。 It basically can be anything that an octave variable can be.它基本上可以是八度变量可以是的任何东西。 For example, the call:例如,调用:

foo ([1 2 3], "string", {678, "other string"}, struct ("over", 9000))

will create an octave_value_list with 4 octave_value s and:将创建一个octave_value_list与4 octave_value S和:

foo ([1 2 3])

will still create an octave_value_list but with a single octave_value .仍将创建一个octave_value_list但只有一个octave_value

The first thing you need is to figure out what you actually have.您需要做的第一件事是弄清楚您实际拥有什么。 You can use octave_value_list.length() to get an idea and you can get individual octave_value s by simply indexing them with round brackets () .您可以使用octave_value_list.length()来获得一个想法,您可以通过简单地用圆括号()对它们进行索引来获得单独的octave_value

for (int i =0; i < list.length (); i++)
    do_stuff_with (list(i));

Once you get an octave_value you can try to convert into many things.一旦你得到一个octave_value你可以尝试转换成很多东西。

std::string   s  = val.string_value ();
bool          b  = val.bool_value ();
boolMatrix    bm = val.bool_matrix_value ();
Cell          c  = val.cell_value ();
NDarray       nd = val.array_value ();
int           i  = val.int_value ();
Array<double> ad = val.vector_value ();

While you can use one of the is_x() methods of octave_value , the recommendation is to check the value of error_state after a conversion to check if anything went wrong.虽然你可以使用的一个is_x()方法octave_value ,该建议是检查的价值error_state转换后,以检查是否出了什么差错。 This will give a more natural Octave experience and will handle conversion between different types that you'd expect in Octave for you.这将提供更自然的 Octave 体验,并将处理您期望在 Octave 中为您提供的不同类型之间的转换。

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

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