필터 지우기
필터 지우기

How to write a changing table name in setdiff function?

조회 수: 1 (최근 30일)
Davin
Davin 2016년 7월 7일
편집: Stephen23 2016년 7월 8일
Hello,
I would like to know how to write a function inside which there is a changing table name.
I have created a table which will change at each loop
eval(['Port' num2str(i-1) '= Portfolio'])
At some point I need to make a difference between this table and a new portfolio so i used the function setdiff
for example, there is a Port1 which is created, now I need to execute this :
[i,j] = setdiff(Port1(:,2), Portfolio(:,2))
Port1 is created by the eval function. Can you tell me how I can incorporate the name of the table dynamically inside the setdiff function which will pick up automatically the newly created table ( port2, port3....). I tried this but its not working :
[i,j] = setdiff(['Port' num2str(i-1)](:,2),Portfolio(:,2))
Thank you very much for your help
D
  댓글 수: 1
Stephen23
Stephen23 2016년 7월 8일
편집: Stephen23 2016년 7월 8일
@Davin: it is almost always a bad idea to create variable dynamically. Read this to know why:
Much simpler and more reliable is to use indexing. You can easily put the tables into cell arrays, and using indexing is then trivial.

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

채택된 답변

Guillaume
Guillaume 2016년 7월 8일
Despite what's been suggested, there is no need to convert the tables to cell arrays (and lose information such as column and row names). Just put the tables into a cell array.
Numbered variables are a clear indication that what is in the variables should be stored together in one container, not as individual variables. In this case a cell array of tables would be the most appropriate.
portnumber = 5;
Port{portnumber} = Portfolio; %store in cell array
[coldiff, loc] = setdiff(Port{portnumber}(:, 2), Portfolio(:, 2))
No eval needed, just straightforward indexing and you still get to manipulate tables with all the advantages that they bring.
  댓글 수: 1
Davin
Davin 2016년 7월 8일
Its exactly what I did, and its far much simpler.
Thanks Guillaume.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Portfolio Optimization and Asset Allocation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by