MATLAB的“错误" (或非常奇怪的行为)与结构和空单元格数组 [英] MATLAB "bug" (or really weird behavior) with structs and empty cell arrays

查看:122
本文介绍了MATLAB的“错误" (或非常奇怪的行为)与结构和空单元格数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是怎么回事.我正在使用R2006b.在我提交错误报告之前,有人使用较新版本的任何机会都可以测试一下,看看他们是否也有同样的行为?

I have no idea what's going on here. I'm using R2006b. Any chance someone out there with a newer version could test to see if they get the same behavior, before I file a bug report?

代码:(bug1.m)

function bug1
S = struct('nothing',{},'something',{});
add_something(S, 'boing');          % does what I expect
add_something(S.something,'test');  % weird behavior
end

function add_something(X,str)
    disp('X=');
    disp(X);
    disp('str=');
    disp(str);
end

输出:

>> bug1
X=
str=
boing
X=
test
str=
??? Input argument "str" is undefined.

Error in ==> bug1>add_something at 11
    disp(str);

Error in ==> bug1 at 4
add_something(S.something,'test');

S.something的空白/虚无似乎允许它移动函数调用的参数.这似乎是非常不良的行为.在短期内,我想找到一个解决之道(我正在尝试创建一个函数,该函数将项目添加到最初为空的单元格数组中,该数组是结构的成员).

It looks like the emptiness/nothingness of S.something allows it to shift the arguments for a function call. This seems like Very Bad Behavior. In the short term I want to find away around it (I'm trying to make a function that adds items to an initially empty cell array that's a member of a structure).

必然的问题:因此,没有办法构造包含任何空单元格数组的struct文字吗?

Corollary question: so there's no way to construct a struct literal containing any empty cell arrays?

推荐答案

您已经发现自己,这不是bug,而是功能".换句话说,这是 STRUCT 的正常行为>功能.如果将空单元格数组作为字段值传递给STRUCT,则假定您要使用具有给定字段名的空结构数组.

As you already discovered yourself, this isn't a bug but a "feature". In other words, it is the normal behavior of the STRUCT function. If you pass empty cell arrays as field values to STRUCT, it assumes you want an empty structure array with the given field names.

>> s=struct('a',{},'b',{})

s = 

0x0 struct array with fields:
    a
    b

要将空单元格数组作为实际字段值传递,您需要执行以下操作:

To pass an empty cell array as an actual field value, you would do the following:

>> s = struct('a',{{}},'b',{{}})

s = 

    a: {}
    b: {}

偶然地,任何您想要使用STRUCT设置单元格数组的字段值的时间都要求您将其包含在另一个单元格数组中.例如,这将创建一个具有包含单元格数组和向量的字段的单个结构元素:

Incidentally, any time you want to set a field value to a cell array using STRUCT requires that you encompass it in another cell array. For example, this creates a single structure element with fields that contain a cell array and a vector:

>> s = struct('strings',{{'hello','yes'}},'lengths',[5 3])

s = 

    strings: {'hello'  'yes'}
    lengths: [5 3]

但这会创建一个包含两个结构元素的数组,分布单元格数组,但复制向量:

But this creates an array of two structure elements, distributing the cell array but replicating the vector:

>> s = struct('strings',{'hello','yes'},'lengths',[5 3])

s = 

1x2 struct array with fields:
    strings
    lengths

>> s(1)

ans = 

    strings: 'hello'
    lengths: [5 3]

>> s(2)

ans = 

    strings: 'yes'
    lengths: [5 3]

这篇关于MATLAB的“错误" (或非常奇怪的行为)与结构和空单元格数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆