结构字段是函数matlab [英] structure field is function matlab

查看:90
本文介绍了结构字段是函数matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在问这个问题,以测试一个概念.我并不想在代码中提出解决方案,我只是需要关于继续前进的方向的建议.

I'm asking this question to test a concept. I'm not trying to have a solution presented in code, I simply need advice as to what direction to continue.

我想创建一个结构字段,该结构字段始终是相同结构的其他字段的函数.

I would like to make a structure field that is always a function of other fields of the same structure.

我已经能够实现可以修改现有结构并使用新字段进行更新的代码.但是,如果不重新初始化代码,这将是不理想的.

I have been able to implement code that can modify the existing structure and update it with a new field. But this doesn't work without re-initializing a code this is not ideal.

我需要能够添加另一个结构,为某些字段提供值,然后通过我定义的函数自动更新其余字段.

I need the ability to add another structure, give it values for certain fields, then automatically update the rest of the fields via functions I have defined.

结构甚至是完成此任务的正确方法吗?我认为不是,但是我不确定可以使用哪种方法.

Is a structure even the right method to accomplish this task? I think it isn't but I am not sure what method can be used.

我已经附上了一个非常简单的代码片段来演示该问题.

I've attached a very simple code snippet to demonstrate the problem.

    module = struct('dim', [ 3 1 0.05], ...
                    'point', [0 0 0],   ...
                     'shape', cubeshape(module.dim,module.point))
                              % cubeshape is my function of dim & point

matlab返回错误....

matlab returns an error....

    Undefined function or variable 'dim'.

这很有意义,因为struct()函数尚未关闭 这意味着模块结构尚未定义.

this makes sense because the struct() function has not yet been closed which means that the module struct has not yet been defined.

如果我的问题太新手了,请告诉我我可以继续进行研究,但是可以提供一些指导.

If my question is too novice, please just tell me I can continue to research, but some guidance would be appreciated.

谢谢!

推荐答案

您可以将'shape'字段设置为然后通过

module.shape()

但是,您会发现,如果在结构中更改module.dim的值,则module.shape()返回的值不会更新.这是因为在实例化时设置了两个函数句柄参数.您可能不想要这个.相反,您可以将module.dimmodule.point作为参数传递给函数句柄:

However, you'll find that if you change the value of module.dim in your struct, the values returned by module.shape() do not get updated. This is because the two function handle parameters gets set at the time of instantiation. You probably don't want this. Instead you can pass module.dim and module.point into your function handle as arguments:

module = struct('dim', [3 1 0.05], ...
                'point', [0 0 0], ...
                'shape', @(dim,point)cubeshape(dim,point))
module.shape(module.dim,module.point)

它不太优美,但是解决了问题,因为将使用module.dimmodule.point的当前值.

It's less graceful, but solves the problem, as the current values of module.dim and module.point will be used.

还有许多其他方法可以解决您的问题.最标准的方法是通过面向对象的方法.但是,有时候,这就像用大锤打苍蝇(

There are numerous other ways to solve your problem. The most standard is via object-oriented approaches. However, sometimes, that can be like swatting a fly with a sledgehammer (a very slow sledgehammer sometimes in Matlab's case). You may be able to do what you need with functions and some re-thinking of your problem.

这篇关于结构字段是函数matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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