필터 지우기
필터 지우기

Generating structure name from a variable

조회 수: 3 (최근 30일)
Bernard
Bernard 2014년 1월 16일
편집: per isakson 2014년 1월 16일
Hello, I have a code that is calculating a bunch of data related to something in a series of somethings. e.g. baseball1...baseball2.... The scripts call for the only input I need is to type 1 or 2 or whatever number I'm looking for. I want to be able to make a structure named baseball1 that I can then save all my data to specific fields. However I cannot type sprintf(baseball%d,'baseballnumber') = struct(); because then it yields 'sprintf' as the name of the structure. It is important to be named that since it will be saved with the series of structures in same folder.

채택된 답변

per isakson
per isakson 2014년 1월 16일
편집: per isakson 2014년 1월 16일
No, you cannot. However, if all the baseballs have the same set of fields you might try something like:
>> baseball.Name = 'a'
baseball =
Name: 'a'
>> baseball(2).Name = 'b'
baseball =
1x2 struct array with fields:
Name
>> baseball(3).Name = 'c'
baseball =
1x3 struct array with fields:
Name
>> {baseball.Name}
ans =
'a' 'b' 'c'
  댓글 수: 2
Bernard
Bernard 2014년 1월 16일
Thanks man, that was my back up plan I guess I'll revert to it.
per isakson
per isakson 2014년 1월 16일
편집: per isakson 2014년 1월 16일
Seriously, I think your back up plan is much better. Why do you prefer "baseball1", etc. ?
Old trick that I had forgotten. It doesn't work with a static workspace:
>> for k = 1:11, assign( sprintf( 'var%d', k ), k ), end
>> assign( sprintf('baseball%d', baseballnumber ), struct() )
where
function assign( Name, Value )
assignin( 'caller', Name, Value );
end
Read this:

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

추가 답변 (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