Matlab Container.Map 行为与 Dictionary

huangapple go评论75阅读模式
英文:

Matlab Container.Map behavior vs Dictionary

问题

I had to remove in my existing script, all dictionary structures to container.Map because I must run my app on old matlab version (<=R2012b)

我不得不在我的现有脚本中删除所有的字典结构,改用container.Map,因为我必须在旧版本的Matlab上运行我的应用程序(<=R2012b)

I have a call member on an instance like this :

我在一个实例上调用成员,就像这样:

% Declaration of container map
app.MC = containers.Map('KeyType', 'double', 'ValueType', 'any');

% Create first instance of Monte Carlo object with the key -15.
% Monte_Carlo is a class herited from Handle
app.MC(-15) = Monte_Carlo(Nb_Iterations_par_depointage, consigne_depointage_y);

% Call member on Monte_Carlo instance
app.MC(-15).Enregistre_Reference_Dephasage_Secteur(a, b);

当我执行这个操作时,我在最后一行得到以下错误:

执行此操作时,我在最后一行收到以下错误消息

Too many output arguments.

输出参数太多。

But if I replace the declaration by

但是,如果我将声明替换为

% Declaration of container map
app.MC = dictionary;

All works fine.

一切都正常。

I checked the types, and in both cases class(app.MC(-15)) returns 'Monte_Carlo'

我检查了类型,在两种情况下,class(app.MC(-15))都返回'Monte_Carlo'

If I proceed in 2 times with an intermediate variable which sotres the app.MC(-15) instance, it works :

如果我分两步进行,使用一个中间变量来存储app.MC(-15)实例,它可以工作:

% Declaration of container map
app.MC = containers.Map('KeyType', 'double', 'ValueType', 'any');

% Create first instance of Monte Carlo object with the key -15
app.MC(-15) = Monte_Carlo(Nb_Iterations_par_depointage, consigne_depointage_y);

% Intermediate variable in order to call member with container.Map
Z = app.MC(-15);

% Now I can do that, and it works
Z.Enregistre_Reference_Dephasage_Secteur(a, b);

现在我可以这样做,它可以工作

Do you know why I can access to the member of the object directly with dictionary, and I can't with container.Map?

你知道为什么我可以直接使用dictionary访问对象的成员,而不能使用container.Map吗?

英文:

I had to remove in my existing script, all dictionary structures to container.Map because I must run my app on old matlab version (<=R2012b)

I have a call member on an instance like this :

% Declaration of container map
app.MC = containers.Map(&#39;KeyType&#39;, &#39;double&#39;, &#39;ValueType&#39;, &#39;any&#39;);

% Create first instance of Monte Carlo object with the key -15. 
% Monte_Carlo is a class herited from Handle
app.MC(-15) = Monte_Carlo(Nb_Iterations_par_depointage, consigne_depointage_y);

% Call member on Monte_Carlo instance
app.MC(-15).Enregistre_Reference_Dephasage_Secteur(a, b);

When I execute this, I get the following error on the last line

Too many output arguments.

But if I replace the declaration by

% Declaration of container map
app.MC = dictionary;

All works fine.

I checked the types, and in both cases class(app.MC(-15)) returns &#39;Monte_Carlo&#39;

If I proceed in 2 times with an intermediate variable which sotres the app.MC(-15) instance, it works :

% Declaration of container map
app.MC = containers.Map(&#39;KeyType&#39;, &#39;double&#39;, &#39;ValueType&#39;, &#39;any&#39;);
    
% Create first instance of Monte Carlo object with the key -15
app.MC(-15) = Monte_Carlo(Nb_Iterations_par_depointage, consigne_depointage_y);
    
% Intermediate variable in order to call member with container.Map
Z = app.MC(-15);

% Now I can do that, and it works 
Z.Enregistre_Reference_Dephasage_Secteur(a, b);

Do you know why I can access to the member of the object directly with dictionary, and I can't with container.Map ?

答案1

得分: 2

这是因为 containers.Map 的实现方式导致的。这是其中许多问题之一,促使他们添加了 dictionary。你没有做错任何事情。

如果

Z.Enregistre_Reference_Dephasage_Secteur(a, b);

修改了 Z,那么 Z 必须是一个句柄对象才能正常工作。如果它不是一个句柄对象(该类派生自 handle),那么在修改后需要将 Z 写回容器,因为你修改的是一个副本:

Z = app.MC(-15);
Z = Z.make_a_change(a, b);
app.MC(-15) = Z;

(但请注意,在你的代码中,Z 要么是一个句柄对象,要么没有被修改,所以这段代码总是正确的。)

英文:

This is simply because of the way that containers.Map is implemented. It’s one of the many issues with it that prompted them to add dictionary. You’re not doing anything wrong.

If

Z.Enregistre_Reference_Dephasage_Secteur(a, b);

modifies Z, then Z must be a handle object for this to work. If it’s not a handle object (the class is derived from handle) then you need to write the Z back into the container after modifying it, because you modified a copy:

Z = app.MC(-15);
Z = Z.make_a_change(a, b);
app.MC(-15) = Z;

(But do note that in your code, Z is either a handle object or it’s not modified, so that code is always correct.)

huangapple
  • 本文由 发表于 2023年8月10日 20:21:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定