以数字方式迭代 MATLAB 结构体

发布于 2024-12-05 22:56:56 字数 569 浏览 7 评论 0原文

是否可以像向量一样以数字方式迭代 MATLAB 结构,而不是使用字段名称?

简而言之,我尝试在 Simulink 的 EML 块内执行以下操作:

S.a.type = 1;
S.a.val = 100;
S.a.somevar = 123;    
S.b.type = 2;
S.b.val = 200;
S.b.somevar2 = 234;
S.c.type = 3;
S.c.val = 300;
S.c.somevar3 = 345;

for i = 1:length(s)
    itemType = S(i).type;
    switch itemType
        case 1
            val = S(i).val * S(i).somevar1;
        case 2
            val = S(i).val * S(i).somevar2;
        case 3
            val = S(i).val * S(i).somevar3;
        otherwise
            val = 0
    end
end

disp(var);

Is it possible to iterate through a MATLAB structure numerically like a vector instead of using the field names?

Simply, I am trying to do the following inside an EML block for Simulink:

S.a.type = 1;
S.a.val = 100;
S.a.somevar = 123;    
S.b.type = 2;
S.b.val = 200;
S.b.somevar2 = 234;
S.c.type = 3;
S.c.val = 300;
S.c.somevar3 = 345;

for i = 1:length(s)
    itemType = S(i).type;
    switch itemType
        case 1
            val = S(i).val * S(i).somevar1;
        case 2
            val = S(i).val * S(i).somevar2;
        case 3
            val = S(i).val * S(i).somevar3;
        otherwise
            val = 0
    end
end

disp(var);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

<逆流佳人身旁 2024-12-12 22:56:56

您应该能够使用 sprintf 动态生成字段名,如下所示:

for i = 1:length(s)
    theFieldName = sprintf('somevar%d', S(i).type);
    val = S(i).val * getfield(S(i), theFieldName);
end

You should be able to generate the fieldnames dynamically using sprintf using something like the following:

for i = 1:length(s)
    theFieldName = sprintf('somevar%d', S(i).type);
    val = S(i).val * getfield(S(i), theFieldName);
end
浮华 2024-12-12 22:56:56

您需要使用字段名称,但您可以动态地这样做。如果您的结构定义为:

s.field1 = 'foo';
s.field2 = 'bar';

那么您可以使用以下任一方式访问字段 field1

s.field1
s.('field1')

您唯一需要的是函数 fieldnames 来动态获取字段名称,以便您的代码示例看起来有点像

elements = fieldnames(S);
for iElement = 1:numel(elements)
   element = S.(elements{iElement});
   itemType = element.type;
   switch itemType
      case 1
          val = element.val * element.somevar1;
      case 2
          val = element.val * element.somevar2;
      case 3
          val = element.val * element.somevar3;

   end
end

如果这些是确切的字段名称,您应该做一些其他事情。首先,您需要重新考虑您的名称,其次您可以使用 Matt 的解决方案的一部分来简化您的代码。

You need to use the field names, but you can do so dynamically. If you have a structure defined as:

s.field1 = 'foo';
s.field2 = 'bar';

Then you can access the field field1 with either

s.field1
s.('field1')

The only thing you need is the function fieldnames to dynamically get the field names such that your code example will look somewhat like

elements = fieldnames(S);
for iElement = 1:numel(elements)
   element = S.(elements{iElement});
   itemType = element.type;
   switch itemType
      case 1
          val = element.val * element.somevar1;
      case 2
          val = element.val * element.somevar2;
      case 3
          val = element.val * element.somevar3;

   end
end

If those are the exact field names, you should do some other things. First of all you would need to rethink your names and secondly you could use part of Matt's solution to simplify your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文