gpt4 book ai didi

matlab - 如何直接从 table.Properties.VariableNames 获取逗号分隔的列表?

转载 作者:太空宇宙 更新时间:2023-11-03 20:00:02 25 4
gpt4 key购买 nike

例子:

>> A = table({}, {}, {}, {}, {}, ...
'VariableNames', {'Foo', 'Bar', 'Baz', 'Frobozz', 'Quux'});
>> vn = A.Properties.VariableNames;
>> isequal(vn, A.Properties.VariableNames)
ans =

1

到目前为止一切顺利,但即使 vnA.Properties.VariableNames 看起来是一样的,但当您尝试获取“逗号”时,它们的行为却大不相同-分隔列表”(使用 {:}):

>> {'Frobnitz', vn{:}}

ans =

'Frobnitz' 'Foo' 'Bar' 'Baz' 'Frobozz' 'Quux'

>> {'Frobnitz', A.Properties.VariableNames{:}}

ans =

'Frobnitz' 'Foo'

有没有一种方法可以直接从 A.Properties.VariableNames 中获取“逗号分隔列表”(也就是说,无需创建像 vn 这样的中间变量)?

(此外,是否有比 isequal 更可靠的函数来测试元胞数组的相等性?在上面的示例中 vnA.Properties.VariableNames 显然不够平等!)


对于那些没有支持(相当新的)table 对象的 MATLAB 版本的人来说,如果使用 dataset 对象(来自统计工具箱)代替。上面的示例将转换为:

clear('A', 'vn');
A = dataset({}, {}, {}, {}, {}, ...
'VarNames', {'Foo', 'Bar', 'Baz', 'Frobozz', 'Quux'});
vn = A.Properties.VarNames;
isequal(vn, A.Properties.VarNames)
{'Frobnitz', vn{:}}
{'Frobnitz', A.Properties.VarNames{:}}

(注意从 VariableNamesVarNames 的变化;省略输出:它与上面显示的输出相同):

最佳答案

isequal没问题. vnA.Properties.VariableNames事实上是平等的。问题是别的……

如果您输入 help dataset.subsref ,你会得到为什么会发生这种情况的解释,这应该与 table 的解释相同。类:

LIMITATIONS:

   Subscripting expressions such as A.CellVar{1:2}, A.StructVar(1:2).field,
or A.Properties.ObsNames{1:2} are valid, but result in subsref
returning multiple outputs in the form of a comma-separated list. If
you explicitly assign to output arguments on the LHS of an assignment,
for example, [cellval1,cellval2] = A.CellVar{1:2}, those variables will
receive the corresponding values. However, if there are no output
arguments, only the first output in the comma-separated list is
returned.

简而言之,当您调用 A.Properties.VarNames{:} 行时,您正在调用 dataset.subsref方法和大括号下标 {:}与其他 . 一起传递给它下标,而不是在调用 dataset.subsref 之后单独应用方法。

因此,看起来您无法直接从 A 获得逗号分隔的列表。不使用中间变量。但是,如果您的目标(如您的示例中所示)是将字符串与另一个字符串连接到一个新的元胞数组中,您可以这样做:

>> [{'Frobnitz'} A.Properties.VarNames]

ans =

'Frobnitz' 'Foo' 'Bar' 'Baz' 'Frobozz' 'Quux'

关于matlab - 如何直接从 table.Properties.VariableNames 获取逗号分隔的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21532915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com