Use string to define variable

7 ビュー (過去 30 日間)
Hugo Policarpo
Hugo Policarpo 2025 年 8 月 9 日
回答済み: Hugo Policarpo 2025 年 8 月 9 日
Hello guys!
I am looking for a way to name a variable with a string name that will be changing according to the T and V values
My code is the following:
-------------------------------
T=15; % input variable T
V=1; % input variable V
load('FRF_T15_V1.mat')
load('FRF_T15_V2.mat')
load('FRF_T15_V3.mat')
formatSpec = 'FRF_T%d_V%d';
filename = sprintf(formatSpec,T,V);
x=eval(filename);
XP=(x{3,3});
save ('XP,'XP')
-----------------------------
I would like for variable XP to be assigned according to the input values of T and V, e.g.,
FRF_T15_V1_XP=(x{3,3});
And then save it as
save (' FRF_T15_V1_XP,' FRF_T15_V1_XP ')
Thanks in advance for your help!
Kind regards,
Hugo
  2 件のコメント
Hugo Policarpo
Hugo Policarpo 2025 年 8 月 9 日
Thanks!

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2025 年 8 月 9 日
編集済み: Stephen23 2025 年 8 月 9 日
Storing meta-data in variable names (or fieldnames) is a very bad way to design your data, which invariably forces you into writing slow, complex, inefficient code that is much harder to debug than if you stored that meta-data as data in a variable (because meta-data is data):
But if you insist on this anti-pattern data design, something like this is probably the least-worst way of doing it:
T = 15;
V = 1;
F = sprintf('FRF_T%d_V%d',T,V);
S = load([F,'.mat']);
C = struct2cell(S);
X = C{1};
S = struct([F,'_XP'],{X{3,3}});
save([F,'_XP.mat'],'-struct',S)
However, treating meta-data as data in its own right (which it is) would be much better data design (it would make your code simpler, more robust, and more efficient).
See also:
  3 件のコメント
Hugo Policarpo
Hugo Policarpo 2025 年 8 月 9 日
your just missing a " ] " in the last line.
Stephen23
Stephen23 2025 年 8 月 9 日
"your just missing a " ] " in the last line."
Thank you, I fixed that.

サインインしてコメントする。

その他の回答 (3 件)

Steven Lord
Steven Lord 2025 年 8 月 9 日
Can you dynamically create variables with numbered names like x1, x2, x3, etc.? Yes.
Should you do this? The general consensus is no. That Discussions post explains why this is generally discouraged and offers several alternative approaches.
  1 件のコメント
Hugo Policarpo
Hugo Policarpo 2025 年 8 月 9 日
Thanks!

サインインしてコメントする。


Meg Noah
Meg Noah 2025 年 8 月 9 日
編集済み: Meg Noah 2025 年 8 月 9 日
There are many ways to do this. Here's one that shows how to make the format of the T and V values have the same width and will sort based on the value of T in the directory. Just change the '%n.n' to the largest width of your number range:
% for the character array version
fname = @(t,v) ['FRF_T' num2str(t,'%3.3d') '_V' num2str(v,'%2.2d') '_XP'];
for T=[1 3 5]
for V=1:2
fprintf(1,'%s\n', fname(T,V));
end
end
FRF_T001_V01_XP FRF_T001_V02_XP FRF_T003_V01_XP FRF_T003_V02_XP FRF_T005_V01_XP FRF_T005_V02_XP
% for the string version
strfname = @(t,v) strcat("FRF_T", num2str(t,'%3.3d'), "_V", num2str(v,'%2.2d'), "_XP");
for T=[1 3 5]
for V=1:2
fprintf(1,'%s\n', strfname(T,V));
end
end
FRF_T001_V01_XP FRF_T001_V02_XP FRF_T003_V01_XP FRF_T003_V02_XP FRF_T005_V01_XP FRF_T005_V02_XP
  1 件のコメント
Hugo Policarpo
Hugo Policarpo 2025 年 8 月 9 日
Thanks!

サインインしてコメントする。


Hugo Policarpo
Hugo Policarpo 2025 年 8 月 9 日
Issue solved!
Thanks for the help!

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by