フィルターのクリア

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 Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by