Name arrays based on a variable

87 次查看(过去 30 天)
mael thevenot
mael thevenot 2019-12-9
评论: Stephen23 2019-12-10
Hi,
I would like to create and fill array, which names are based on a variable.
Right now I manage to create and fill arrays, simply like this :
This_array(1) = 8;
This_other_array(4) = 12;
So I create the array and at the same time fill the index of the array that I want.
But what I'm trying to do is like that, to give an idea :
var = 'B'
[var, This_array](1) = 8
[var, This_other_array](4) = 8
So this do not work it's just an exemple to illustrate what I'm tryiing to do, like dynamically change the name of created arrays based on a variable.
Here the variable var is a char (idk why but that's what "class" tell me).
This code is in a for loop, an I would like to obtain an array called "B_This_array", or "C_This_array" based on the variable.
I've tried numerous ways and can't manage to make it work.
Thanks.
  1 个评论
Stephen23
Stephen23 2019-12-10
"...dynamically change the name of created arrays based on a variable."
Which is one way that beginners force themselves into writing slow, complex, buggy code:
It seems that you are trying to force meta-data into a variable name. Meta-data is data, and data should be stored in variables, not in variable names.
Note that indexing is neat, simple, and very efficient. You should probably use indexing.

请先登录,再进行评论。

回答(1 个)

Rik
Rik 2019-12-9
The reasons why naming variables dynamically can be found here. It sounds like you are looking for the features a struct will provide you.
  3 个评论
Rik
Rik 2019-12-10
This_array(1) = 8;
This_other_array(4) = 12;
B.This_array=This_array;
B.This_other_array=This_other_array;
C.This_array=This_array;
C.This_other_array=This_other_array;
The point is that you shouldn't be generating variable names. You can get a bit closer to that like this:
This_other_array=struct;%overwrite in case the variable already exists
This_other_array.B=1;
This_other_array.C=[3 1];
You can treat the fields as their own variable. You can even use a char array to index into it, but you can generally avoid that.
This_other_array=struct;%overwrite in case the variable already exists
This_other_array.B=1;
This_other_array.C=[3 1];
var='B';
clc,disp(This_other_array.(var))
Stephen23
Stephen23 2019-12-10
mael thevenot's "Answer" moved here:
Ok so I managed to do what I wanted with nested structures and indexing. I created structure and then dynamicaaly access its field with :
fns = fieldnames(A);
A.(fns{3})
So with nested structs I have things like :
if ....
var 1 = 2;
fns2 = fieldnames(s.(fns{var1}));
var 2 = 4;
fns3 = fieldnames(s.(fns{va1}).(fns2{var2}));
...
end
s.(fns{var1})(1).(fns2{var2})(1).(fns3{var3})(1).(fns4{var4})(1) = my_value;
And actually it's way more neat and short to do like this :)
Thank you very much for your help !

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by