English 中文(简体)
我如何能动地进入实地?
原标题:How do I access structure fields dynamically?

我有一个结构,有多个领域,它们都是不同长度的病媒。 我谨按顺序进入这些地区。 我曾尝试过如下,但MATLAB也这样做。 我如何能够这样做?

S = struct( A , [1 2],  B ,[3 4 5]);
SNames = fieldnames(S);
for loopIndex = 1:2
  field = getfield(S, SNames(loopIndex));
  %do stuff w/ field
end
??? Index exceeds matrix dimensions

首先是使用结构的Im,因为一个阵列会遇到不同外地时间的麻烦。 是否有更好的替代办法?

最佳回答

热能动的实地参考,在括号中,你在界定stuff

S = struct( A , [1 2],  B ,[3 4 5]); 
SNames = fieldnames(S); 
for loopIndex = 1:numel(SNames) 
    stuff = S.(SNames{loopIndex})
end 

我同意Steve和Adam。 使用室。 这对处于其他局势中的人们来说是正确的,尽管如此!

问题回答

实地做法是征兆(尽管现在我还没有MATLAB,但我不清楚上述工作为什么会失败)。

关于替代数据结构,你还可能希望研究MATLAB的电池阵列。 它们还使你能够储存和指数化不同的病媒。

你们可以使用殖民地标志来避免指数化:

S = struct( A , [1 2],  B ,[3 4 5]); 
SNames = fieldnames(S); 
for SName = [SNames{:}]
    stuff = S.(SName)
end

如果你需要使用我所认为行之有效的结构,即首先改用一个囚室,那么你就拥有两个世界的最佳选择。

S = struct( A , [1 2],  B ,[3 4 5]); 
S_Cell = struct2cell(S);
%Then as per gnovice
for loopIndex = 1:numel(S_Sell)   % Loop over the number of cells
    array = S{loopIndex};         % Access the contents of each cell
    %# Do stuff with array
end

我用一些类似东西来做一些在建构中产生的事情,然后我需要把它当作一个矩阵,在这种情况下,它就像简单一样。

M = cell2mat(struct2cell(S));

转换成矩阵

只是为混合添加另一个答案。 我喜欢“尼佛”的解决办法,但只有使用单一字母名称的田地。 我使用的解决办法是:

S = struct( A , [1 2],  B ,[3 4 5],  Cee , [6 7]); 
for SName = fieldnames(S) 
    stuff = S.(SName{1})
end

。 每一个地方,SName成为1x1个囚室,因此,我们使用内容索引,以获取第一组和唯一包含<代码>的内容。 SName{1}。





相关问题
MATLAB Solving equations problem

I want to solve these equations using MATLAB and I am sure there is a non zero solution. The equations are: 0.7071*x + 0.7071*z = x -0.5*x + 0.7071*y + 0.5*z = y -0.5*x - 0.7071*y +...

Difference between MATLAB s matrix notations

How do you read the following MATLAB codes? #1 K>> [p,d]=eig(A) // Not sure about the syntax. p = 0.5257 -0.8507 -0.8507 -0.5257 d = ...

preventing data from being freed when vector goes out of scope

Is there a way to transfer ownership of the data contained in a std::vector (pointed to by, say T*data) into another construct, preventing having "data" become a dangling pointer after the vector goes ...

Divide two polynomials using MATLAB

I want to divide p(x) by q(x) given that: p(x)=-5x^4+3x^2-6x q(x)=x^2+1 I tried: p=inline( -5*(x^4)+3*(x^2) , x ) p = Inline function: p(x) = -5*(x^4)+3*(x^2) q=inline( x^2+1 , x ) q = ...

matlab deals with Fibbonacci

The Fibonacci series is given as follows: 1, 2, 3, 5, 8, 13, 21, ... How can I write a script which calculates and prints the n-th Fibonacci term (for n>2), where n is inputed by the user. This ...

How do I plot lines between all points in a vector?

I have a vector containing some points in 2-D space. I want MATLAB to plot these points with lines drawn from every point to every other point. Basically, I want a graph with all vertices connected. ...

How do I create a string using a loop variable in MATLAB?

I have a loop like this: for i=1:no %some calculations fid = fopen( c:\out.txt , wt ); %write something to the file fclose(fid); end I want data to be written to different files like ...