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 Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by