How to populate a new variable from an old one
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Stephen23
am 1 Jun. 2021
"I have have had to create a dynamic variable..."
I doubt that. What is much much more likely is this:
Antworten (1)
Jan
am 1 Jun. 2021
Bearbeitet: Jan
am 1 Jun. 2021
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 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!