So first I declare some variables:
data.variable=12
v1='variable'
n=1
The results:
strcat('data.', v1)
data.variable
But
strcat('data.', (strcat('v',num2str(n))))
data.v1
Instead of data.variable. I'm assuming that this is because v1 is a string. What should I do to make the second command return data.variable?

 採用された回答

per isakson
per isakson 2013 年 1 月 24 日

1 投票

You might find dynamic field names useful. Doc says:
Generate Field Names from Variables
[...]
For example, create a field name from the current date:
currentDate = datestr(now,'mmmdd');
myStruct.(currentDate) = [1,2,3]

その他の回答 (2 件)

Evgeny Pr
Evgeny Pr 2013 年 1 月 24 日

0 投票

So that's that?
strcat('data.', evalin('base', strcat('v',num2str(n))))
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 1 月 24 日

0 投票

Why are n't you using one array to save your variables, instead of using, v1,v2,...
For example
data.variable1=10;
variable={'variable1','variable2','variable3'}
out=data.(variable{1})

2 件のコメント

Gregory
Gregory 2013 年 1 月 24 日
That's probably a better way to do it; I'm learning matlab as I code, I'll try this as well. Thanks.
Jan
Jan 2013 年 1 月 24 日
And you can decrease the level of indirection further by using data.variable{1} directly instead of hiding the index in the fieldname.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Structures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by