为什么在分配结构之前无需定义结构域? [英] Why is there no need to define fields of structures before assigning them?

查看:136
本文介绍了为什么在分配结构之前无需定义结构域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用MATLAB中的其他人的代码工作,看来他正在创建结构,只是通过使用字段名称而根本没有声明它们.这就是在MATLAB中的工作方式,您只是开始使用所选的不区分大小写的字段名称?

I am working with somebody else's code in MATLAB and it looks like he is creating structures, just by using field names without declaring them at all. Is that how it works in MATLAB, you just start using case-insensitive field names of your choice?

例如,他有这样的东西:

So, for example, he has something like this:

classdef Emitter
   properties
      transients=[];
   end
end

... some other class
   methods
      function sound=makeSound()
         emitterthing.transients.receivedIntensity = 100
         emitterthing.transients.frequency = 500
      end
   end 

换句话说,他只是开始组成字段名称并为其分配值,而无需声明字段名称或其类型.

In other words, he just starts making up field names and assigning values to them without declaring the field names or their type.

在MATLAB中是这样工作的吗?

Is that how it works in MATLAB?

推荐答案

是,字段名称唯一的限制是,如果您具有结构的 array ,则它们都必须具有相同的字段名称.

The only constraint is that if you have an array of structs, they all have to have the same field names.

%// Create an array of structures
S(1).one = '1.1';
s(2).one = '1.2';

%// Dynamically add a new field to only one of the structs in the array
s(1).two = '2.1';

%// s(2) automatically gets a "two" field initialized to an empty value
disp(s(2))

%//     one: '1.2'
%//     two: []

此外,MATLAB使用动态类型,因此无需提前定义任何变量的类型或struct的字段.

Also, MATLAB uses dynamic typing so there is no need to define the type of any variable or the fields of a struct ahead of time.

这篇关于为什么在分配结构之前无需定义结构域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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