Using a string variable as a variable name in an equation.

조회 수: 7 (최근 30일)
Jaxson Bonsall
Jaxson Bonsall 2023년 11월 20일
편집: Stephen23 2023년 11월 21일
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
Stephen23
Stephen23 2023년 11월 21일
편집: Stephen23 2023년 11월 21일
"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.

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

답변 (2개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 11월 20일
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)
Plex_Add{1} = 58 59 61 62 64 65 67 68 18 21 Plex_Add{2} = 13 57 60 63 66 Plex_Add{3} = 13 57 60 63 66 179
  댓글 수: 2
Jaxson Bonsall
Jaxson Bonsall 2023년 11월 20일
No thats not what I am trying to do. I am trying to use the for loop instead of the 3 lines above that to do the same thing but the for loop does not work because the equation in the for loop does not recognize Plex_Nms{i} as ELEC, FO, or HVAC so that those vectors can be used in the equation.
Voss
Voss 2023년 11월 20일
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);
Plex_Add{1} = 58 59 61 62 64 65 67 68 18 21 Plex_Add{2} = 13 57 60 63 66 Plex_Add{3} = 13 57 60 63 66 179

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


Voss
Voss 2023년 11월 20일
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);
ELEC: [10×1 double] FO: [5×1 double] HVAC: [6×1 double]
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);
ELEC: [10×1 double] FO: [5×1 double] HVAC: [6×1 double]
disp(S_new.ELEC);
58 59 61 62 64 65 67 68 18 21
disp(S_new.FO);
13 57 60 63 66
disp(S_new.HVAC);
13 57 60 63 66 179

카테고리

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by