필터 지우기
필터 지우기

Retrive cell names from structure into a string vector

조회 수: 14 (최근 30일)
Mikkel
Mikkel 2019년 2월 12일
편집: Stephen23 2019년 2월 12일
Hi
I have a structure that contains cell's inside. I would like to retrive the name of the cells into a vector, so I can make a for loop. This data is just for one year, and I have alot, where the dates change, so I cant just write the dates down since they are generated automaticly. The cell array inside each date is the same.
Dates = ['April_24_2018';'May_01_2018';'May_08_2018'... %And so on.]
for h = 1:size(Dates,1)
Data = Input.Dates(h,:){1,2}(1,1);)
end
Can't find a guide to do this, can somebody help :) ?
Best Regards Mikkel
Capture.PNG
  댓글 수: 3
Mikkel
Mikkel 2019년 2월 12일
Hi Jan
How do I make a code that can access the Input.April_24_2018{1,3}(1,1) without having to write the April_24_2018 ? I would like to have something like. Input.(1){1,3}(1,1)...
Stephen23
Stephen23 2019년 2월 12일
편집: Stephen23 2019년 2월 12일
@Mikkel: awkwardly forcing meta-data into fieldnames is not likely to make your code very neat or efficient. A non-scalar structure would make it trivially easy to loop over the elements, using basic indexing:
S(1).time = [2019,02,12];
S(1).data = {...};
S(2).time = [2019,02,12];
S(2).data = {...};
...
for k = 1:numel(S)
S(k).time
S(k).data{1}(1)
end

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

채택된 답변

Adam Danz
Adam Danz 2019년 2월 12일
  댓글 수: 4
Steven Lord
Steven Lord 2019년 2월 12일
Alternately if you just want to iterate through the fields in the struct array consider using structfun.
Adam Danz
Adam Danz 2019년 2월 12일
편집: Adam Danz 2019년 2월 12일
@Mikkel, now that your question has become clearer, you've got some decisions to make.
Using a non-scalar structure would really clean things up (as suggested by Stephen Cobeldick). You could convert your existing data to this format or, if possible, you could change the existing code that generated the structure in the first place.
If you decide not to go that route, two good options are structfun at dynamic field names (as suggested by Jan and Steven Lord. If you're just applying the same function to all fields and all of your fields are organized in the same way, go with structfun(). Otherwise, go with dynamic field names (or a combination of both).
If you get stuck, feel free to follow-up. Providing a sample of data always helps.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by