首页 > 解决方案 > Running a for loop through 5 data structs

问题描述

I have been searching for simple solutions for running a for loop through my structs, but the code I've generated gives me an error indicating that my struct isn't defined within the function. I have 20 structs (sss(1:20) in example), and within each of these, I have 5 structs I want to combine.

My problem stated simply is that I want everywhere there is an "x" in the for function to be run through 20 times for the numbers 1-20 for each different struct name. This is probably a simple problem, but I can't find the simple solution. Help would be much appreciated

I've tried using "fieldnames" function, but it didn't work. perhaps I wasn't using it properly?

for x = 1:20
   a = union(s1.ss1.sss_{x}.d1,s1.ss1.sss_{x}.d2)
   b = union(a,s1.ss1.sss_{x}.d3)
   c = union(b,s1.ss1.sss_{x}.d4)
   newstruct_(x) = union(c,s1.ss1.sss_{x}.d5) 
end

标签: matlabfor-loopstructmergestructure

解决方案


推荐阅读