Changing name of structures

조회 수: 31 (최근 30일)
esteban calderon
esteban calderon 2018년 9월 11일
댓글: Paul Shoemaker 2018년 9월 12일
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
Rik
Rik 2018년 9월 11일
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
Stephen23 2018년 9월 12일
" 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.

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

채택된 답변

Paul Shoemaker
Paul Shoemaker 2018년 9월 11일
Hello Esteban,
I agree that the naming of these structures is unfortunate. You would be better off structuring your data like so:
data(1).prices = someData;
data(2).prices = someMoreData;
...
data(n).prices = nData;
Or even like this (depending on the content of prices)
prices(1) = someData; % SomeData can be a structure as long as follow-on indexes have same fields
prices(2) = someMoreData; % Must have same fields as index 1
...
prices(n) = nData;
If your data was one level deeper in your structures then you could also take advantage of dynamic field names.
To illustrate dynamic field referencing, let's assume you had your data nested under a parent structure called "data." You would then have fields within data called data.structure_portfolio1_prices, data.structure_portfolio2_prices, and so on. In this case, you could then reference them without resorting to eval, like so:
for idx = 1:10
tempData = data.(['structure_portfolio' num2str(idx) '_prices']);
% Do some operation on "tempData"
...
end
With dynamic fields, you reference (or create) fields using parentheses.
Hope this helps!
Paul Shoemaker
  댓글 수: 2
Stephen23
Stephen23 2018년 9월 12일
"You would then have fields within data called data.structure_portfolio1_prices, data.structure_portfolio2_prices"
Using indexing would still be much simpler and more efficient than dynamically creating those fieldnames.
Paul Shoemaker
Paul Shoemaker 2018년 9월 12일
I agree completely. I'm just offering different approaches. Besides, it appeared from Esteban's original post that he perhaps was not familiar with dynamic field referencing, which is a useful skill to have in the ol' toolbox!

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by