Using a string variable as a variable name in an equation.
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello this is my current code which works fine but I want the second portion to be more automated. The main thing I am unsure of here is how to use a cell value from a vector of strings as a variable name in an equation later on.
Plex_Nms = [{'ELEC'}; {'FO'}; {'HVAC'}];
Diff_Value = 3;
ELEC = [61 62 64 65 67 68 70 71 21 24]';
FO = [16 60 63 66 69]';
HVAC = [16 60 63 66 69 182]';
Currently the only way I can get my code to operate is using the 3 lines below but I would rather do this in a for loop.
Plex_Add_VCs{1} = ELEC - Diff_Value;
Plex_Add_VCs{2} = FO - Diff_Value;
Plex_Add_VCs{3} = HAVC - Diff_Value;
I would like it to worklike this instead of the 3 lines above but the for loop does not work.
for i = 1:3
Plex_Add{i} = Plex_Nms(i) - Diff_Value;
end
I am unsure of how to get Plex_Nms(i) to act as if I am using the ELEC variable name.
1 comentario
Stephen23
el 21 de Nov. de 2023
Editada: Stephen23
el 21 de Nov. de 2023
"The main thing I am unsure of here is how to use a cell value from a vector of strings as a variable name in an equation later on."
Accessing variable names like that is one way that users force themselves into writing slow, complex, inefficient, insecure, fragile, obfuscated code that is buggy and hard to debug. Best avoided.
Use a container array, e.g. structure, table, cell array. Then use indexing. Indexing is simple and efficient.
Respuestas (2)
Sulaymon Eshkabilov
el 20 de Nov. de 2023
If understood your question correctly, is this what you are trying to solve for:
Diff_Value = 3;
ELEC = [61 62 64 65 67 68 70 71 21 24]';
FO = [16 60 63 66 69]';
HVAC = [16 60 63 66 69 182]';
Plex_Nms = [{ELEC}, {FO}, {HVAC}];
Plex_Add_VCs{1} = ELEC - Diff_Value;
Plex_Add_VCs{2} = FO - Diff_Value;
Plex_Add_VCs{3} = HVAC - Diff_Value;
for i = 1:3
Plex_Add{i} = Plex_Nms{i} - Diff_Value;
end
% To display/see the found solutions
celldisp(Plex_Add)
2 comentarios
Voss
el 20 de Nov. de 2023
I think it works. Just remove those three lines you don't need.
Diff_Value = 3;
ELEC = [61 62 64 65 67 68 70 71 21 24]';
FO = [16 60 63 66 69]';
HVAC = [16 60 63 66 69 182]';
Plex = {ELEC; FO; HVAC};
Plex_Add = cell(size(Plex));
for i = 1:numel(Plex)
Plex_Add{i} = Plex{i} - Diff_Value;
end
% To display/see the found solutions
celldisp(Plex_Add);
Voss
el 20 de Nov. de 2023
You can put those vectors into a struct, using their names as fields:
Plex_Nms = {'ELEC'; 'FO'; 'HVAC'}; % names
ELEC = [61 62 64 65 67 68 70 71 21 24]';
FO = [16 60 63 66 69]';
HVAC = [16 60 63 66 69 182]';
Plex_Vls = {ELEC; FO; HVAC}; % values
% create a struct S:
args = [Plex_Nms Plex_Vls].';
S = struct(args{:});
disp(S);
Then create a new struct with the same fields and decrease the values by Diff_Value:
Diff_Value = 3;
S_new = S;
fn = fieldnames(S);
for ii = 1:numel(fn)
S_new.(fn{ii}) = S.(fn{ii}) - Diff_Value;
end
disp(S_new);
disp(S_new.ELEC);
disp(S_new.FO);
disp(S_new.HVAC);
0 comentarios
Ver también
Categorías
Más información sobre Financial Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!