Changing name of structures
Show older comments
Hi Everybody,
i would like to make a question about the use of structures, i have loaded on workspace a few structures but those have different name, for example structure_portfolio1_prices, structure_portfolio2_prices and so on, i need to make the same operations in all structures so i tried to use a "for" changing the name of the structure something like 'structure_portfolio',num2str(i),'_prices'; but when i try to modifie one of the contents of the structure matlab doesn´t recognize that name as the name of the structure rather just as an string. Thanks a lot
2 Comments
Rik
on 11 Sep 2018
You have made a trivial job very hard by using number variables. You shouldn't use numbered variables, as that will force you to use eval. I would recommend fixing your data at the source: don't number them when you create them.
Stephen23
on 12 Sep 2018
" i have loaded on workspace a few structures but those have different name, for example structure_portfolio1_prices, structure_portfolio2_prices and so on..."
And that is the problem right there. When beginners dynamically access variable names then they force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know why:
Indexing is much simpler and much more efficient. You should use indexing. Possibly you could avoid this whole situation just by load-ing into an output variable:
S = load(...)
but it depends on how you are getting all of those structures into your workspace, which you have not told us anything about.
Accepted Answer
More Answers (0)
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!