Issues with looping in structure: variable in the name

I am loading one Excel spreadsheet with 11 tabs. The 11 tabs are named fl1, fl2,... fl11.
The spreadsheet is currently loaded into a structure named flight_data. I don't like the way it is organized, so I want to create a 11 new structures: somename1, somename2,....somename11.
I think the code will show how I'm trying to structure my data. The goal is to have 11 strutures that look like this:
%my loop that doesn't work
for i = 1:11
fpro(i).loc7.x = flight_data.fpro{i}(:,1)
end
%my structure setup
fpro(i).name = 'Flight Profile {i}';
fpro(i).loc7.x = flight_data.fl{i}(:,1)
fpro(i).loc9.x = flight_data.fl{i}(:,4)
fpro(i).loc10.x = flight_data.fl{i}(:,7)
fpro(i).loc7.y = flight_data.fl{i}(:,2)
fpro(i).loc9.y = flight_data.fl{i}(:,5)
fpro(i).loc10.y = flight_data.fl{i}(:,8)
fpro(i).loc7.z = flight_data.fl{i}(:,3)
fpro(i).loc9.z = flight_data.fl{i}(:,6)
fpro(i).loc10.z = flight_data.fl{i}(:,9)

댓글 수: 2

Stephen23
Stephen23 2024년 8월 22일
편집: Stephen23 2024년 8월 22일
"...I want to create a 11 new structures: somename1, somename2,....somename11."
Best avoided:
Rather than inefficiently forcing pseudo-indices into variable names just use actual indices into e.g. a structure array. Real indexing will be simpler and much more efficient thatn what you are attempting. Your approach will make working with your data more complex and inefficient.
You are 100% correct...no excuses!

댓글을 달려면 로그인하십시오.

 채택된 답변

Voss
Voss 2024년 8월 22일
I guess this is something like your flight_data variable:
flight_data = struct('fl',{permute(num2cell(rand(10,9,11),[1 2]),[3 1 2])})
flight_data = struct with fields:
fl: {11x1 cell}
flight_data.fl
ans = 11x1 cell array
{10x9 double} {10x9 double} {10x9 double} {10x9 double} {10x9 double} {10x9 double} {10x9 double} {10x9 double} {10x9 double} {10x9 double} {10x9 double}
I think you're going for this:
clear('fpro')
for i = 1:numel(flight_data.fl)
fpro(i).name = sprintf('Flight Profile %d',i);
fpro(i).loc7.x = flight_data.fl{i}(:,1);
fpro(i).loc9.x = flight_data.fl{i}(:,4);
fpro(i).loc10.x = flight_data.fl{i}(:,7);
fpro(i).loc7.y = flight_data.fl{i}(:,2);
fpro(i).loc9.y = flight_data.fl{i}(:,5);
fpro(i).loc10.y = flight_data.fl{i}(:,8);
fpro(i).loc7.z = flight_data.fl{i}(:,3);
fpro(i).loc9.z = flight_data.fl{i}(:,6);
fpro(i).loc10.z = flight_data.fl{i}(:,9);
end
Check some of the results:
fpro
fpro = 1x11 struct array with fields:
name loc7 loc9 loc10
fpro(1)
ans = struct with fields:
name: 'Flight Profile 1' loc7: [1x1 struct] loc9: [1x1 struct] loc10: [1x1 struct]
fpro(2)
ans = struct with fields:
name: 'Flight Profile 2' loc7: [1x1 struct] loc9: [1x1 struct] loc10: [1x1 struct]

댓글 수: 2

I had to make a simple adjustment, but it worked. Thanks.
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2024a

질문:

Joe
2024년 8월 22일

댓글:

2024년 8월 23일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by