无法访问MATLAB中的结构字段

时间:2011-06-20 20:11:29

标签: matlab matlab-struct

我在MATLAB中有一个结构。当我尝试访问某个字段时,我看到显示:

[4158x5 double]

如何获取阵列本身?

1 个答案:

答案 0 :(得分:1)

我的猜测是存储在结构字段中的矩阵封装在cell array中,因此您需要use curly braces {} to index the cell contents(即内容索引)。考虑这个例子:

>> S.field1 = {1:5};  %# Create structure S with field 'field1' containing a cell
                      %#   array which itself contains a 1-by-5 vector
>> S.field1           %# Index just the field...

ans = 

    [1x5 double]      %# ...and you see the sort of answer you were getting

>> S.field1{1}        %# Index the field and remove the contents of the cell...

ans =

     1     2     3     4     5  %# ...and now you get the vector

注意:在较新版本的MATLAB中,显示的内容略有不同,这避免了这种混淆。以下是您现在所看到的内容:

>> S.field1

ans =

  cell    % Note now that it displays the type of data

    [1×5 double]