How to populate a new variable from an old one
조회 수: 1 (최근 30일)
이전 댓글 표시
Good morning,
I have have had to create a dynamic variable (I am well aware of the posts about this) to prevent the cell content being overwritten during a save and load cycle. What I would like to do is take the core variable and transfer its content to the new one before it is overritten.
I am having the chicken and egg probelm, I cannot isolate the new variable exclusively.
eval(['IN_D_mat_G_sqz_' char(SA_type)]); % Appends var name with slope angle
WS = who;
IN_index = find(contains(WS,'IN_D_mat_G_sqz')); % finds in the workspace core and new var
% Now I am stuck
댓글 수: 1
Stephen23
2021년 6월 1일
"I have have had to create a dynamic variable..."
I doubt that. What is much much more likely is this:
답변 (1개)
Jan
2021년 6월 1일
편집: Jan
2021년 6월 1일
You are aware of the warning about creating variables dynamically. So why do you try to do this? This method is a shot in you kneed and "now I am stuck" is exactly what is expected.
I have over 40 years of experiences in programming and I would stuck with this approach also. Even if I'd find a way, it would slow down the processing speed massively and increase the level of complexity until the code can not be debugged efficiently anymore.
The name "IN_D_mat_G_sqz_" sounds like too much information is hidden in the name of the variable. This is one of the typical problem of the dynamic creation.
The problem will not occur, if you store the information e.g. in fields of a struct instead:
data(1).value = 1:17;
data(1).conditions = {'IN', 'D', 'G', 'sqz'};
data(1).type = char(SA_type);
The finding a "free variable" is done by:
numel(data) + 1
That you are stuck with the dynamic creation of variables is a secure makrer, that it is time to refactor your code. Therefore this is a really good question. Only the level of the solution differs from what you expect.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!