【发布时间】:2014-07-03 21:50:52
【问题描述】:
Matlab 的语法令人愤怒,尤其是结构体。在 Bioinformatics 工具包中,有一个名为 jcampread(File) 的方法,描述为 here。
在描述中,方法 jcampread() 采用 Filepath 并输出到名为 jcampStruct 的结构中。据我了解,在 Matlab 中,您不会像在 C 中那样声明返回变量类型:您只需给返回变量一个名称,它就知道 jcampread() 方法的返回将是一个 jcampStruct。它是怎么做的,我不知道,但确实如此。
我在示例部分的第 4 步中完全按照他们的示例显示的方式输入了代码,我从 Matlab 收到以下错误消息:
Incorrect number of right hand side elements in
dot name assignment. Missing [] around left hand
side is a likely cause.
Error in jcampread>ntupleRead (line 510)
dataBlock.ZName = name{Zidx};
Error in jcampread (line 192)
dataBlocks = ntupleRead(fid);
This 站点表示“当 f 具有多个矩阵元素时”会出现问题。代码如下:
»f.a = [1 0]
f =
a: [1 0]
»f.b = [1 2]
f =
a: [1 0]
b: [1 2]
»f = setfield(f,'a',[2 2])
f =
a: [2 2]
b: [1 2]
»f(2).a=1
f =
1x2 struct array with fields:
a
b
»f = setfield(f,'a',[2 2])
??? Error using ==> setfield
Incorrect number of right hand side elements in dot name assignment.
Missing [] around left hand side is a likely cause.
我假设这意味着矩阵 f 看起来像这样:
f = [ [a1; b1]; [a2; b2]; ]
f = [ [[2 2]; [1 2]]; [[1]; []]; ]
当他们试图更新设置为
的 f.a 时 f.a = [[2 2]; [1]]
...对于单个元素 [2 2],它不喜欢这样,因为 f.a 当前是具有 2 个向量元素的矩阵。基本上,如果您要重新分配 f.a(矩阵 f 的属性 a 的所有元素),则必须重新分配 f.a 以使其具有与当前相同数量的元素。
我认为这就是 setfield 示例中出现此错误的原因。
我的问题:这如何应用于 jcampread()? jcampStruct 从字面上看是具有相同属性的结构,并且这些属性只分配一次。我不明白:
一个。 matlab 如何知道 jcampread() 的返回值是一个 jcampStruct,并且 湾。为什么(鉴于它知道 (a)),“右手数不正确..”错误消息在这里触发。
谁能帮我解决这个问题?
【问题讨论】:
-
只是一个站点注释:Matlab 中没有嵌套数组。
[ [a1; b1]; [a2; b2]; ]是另一种写法[ a1; b1; a2; b2; ] -
您也不能创建不均匀的矩阵。因此,当您尝试更新矩阵时,
f.a所拥有的内容将不起作用,因此您提到的内容确实是正确的。另外,尝试直接分配f.a = ...而不是使用setfield。
标签: matlab data-structures matrix multidimensional-array