Given that the use of dynamic variable is a bad practice (as stated in the above comments), among the possible "alternative" solution (these also, already suggested in the above comments) the use of the struct
data type can be considered coupled with the use of the dynamic field names
.
The advantage of the dynamic field names
consists in having the possibility to "programmatically" define the names of the variables (which in this case will be the field of the struct) avoiding the potential danger implied by eval
.
Also the use of a struct
allows to manage its field and their content programmatically by using the very large set of related functions.
In the following "crazy" implementation, the dynamic field names
concept is used to create a set of variables as fields of a struct.
In particular, the code allows to create:
a set of variables (struct's field) whose name are defined in a cellarray:
dynamic_vars={'Bucket','another_var','one_more_var'}
for each of the variables, is then possible to specify the number of (that is, for example, 4 Bucket => Bucket_1, Bucket_2, ...
. The quantity is specified in an array
how_many=[4 3 2]
- then it is possible to specify, in a cellarray the type of the variables (
double
, char
, cell
)
var_type={'double' 'char' 'cell'}
- for each of the above varaibles / struct's field is possible to specify a way to initialize them through functions such as
zeros
, nan
, ones
or a string
init_set={'NaN' 'Hellp World' 'zeros'}
- to complete the definition and initialization of the variables, it is possible to set their
size
using a cellarray:
var_dim={[2 3] -1 [1 3] [2 3]}
This is the full code:
% Define the name of the variable
dynamic_vars={'Bucket','another_var','one_more_var'}
% Define how many variables to be created with the "dynamic_vars" names
how_many=[4 3 2]
% Define the type of the variable to be created
var_type={'double' 'char' 'cell'}
% Define the function or string to be used to initialize the variables
init_set={'NaN' 'Hellp World' 'zeros'}
%init_set={'NaN' 'Hellp World' 'char'}
% Define the size of the dynamic_vars tobe created
% In the case of cellarray:
% if cellarray of "number" two dimension have to be provided:
% size of the cellarray
% size of the content of the cellarray
% if cellarray of string to specify:
% the size of the cellarray
% the string to be used to initialize the cellarray
var_dim={[2 3] -1 [1 3] [2 3]}
%var_dim={[2 3] -1 [1 3] 'dummy_str'}
n_var=length(dynamic_vars)
% Loop over the variables to be created
for i=1:n_var
% Loop over the number of variables to be created
for j=1:how_many(i)
% Select the data type of the variable
switch(var_type{i})
% Create the i-th variable of the j-th type and iknitialize it
case 'double'
switch(init_set{i})
case 'zeros'
my_data.([dynamic_vars{i} '_' num2str(j)])=zeros([var_dim{i}])
case 'NaN'
my_data.([dynamic_vars{i} '_' num2str(j)])=nan([var_dim{i}])
case 'ones'
my_data.([dynamic_vars{i} '_' num2str(j)])=ones([var_dim{i}])
case 'rand'
my_data.([dynamic_vars{i} '_' num2str(j)])=rand([var_dim{i}])
otherwise
disp('ERROR: Unvalid init_set')
return
end
case 'char'
my_data.([dynamic_vars{i} '_' num2str(j)])=init_set{i}
case 'cell'
switch(init_set{i})
case 'char'
my_data.([dynamic_vars{i} '_' num2str(j)])=repmat({var_dim{i+1}},[var_dim{i}])
case 'zeros'
my_data.([dynamic_vars{i} '_' num2str(j)])=repmat({zeros(var_dim{i+1})},[var_dim{i}])
case 'NaN'
my_data.([dynamic_vars{i} '_' num2str(j)])=repmat({nan(var_dim{i+1})},[var_dim{i}])
case 'ones'
my_data.([dynamic_vars{i} '_' num2str(j)])=repmat({ones(var_dim{i+1})},[var_dim{i}])
case 'rand'
my_data.([dynamic_vars{i} '_' num2str(j)])=repmat({rand(var_dim{i+1})},[var_dim{i}])
otherwise
disp('ERROR: Unvalid init_set')
return
end
otherwise
disp('ERROR: Unvalid data type')
return
end
end
end
my_data
which generate the struct my_data
with the following fields:
Bucket_1
Bucket_2
Bucket_3
Bucket_4
another_var_1
another_var_2
another_var_3
one_more_var_1
one_more_var_2
Initialized as follows:
Bucket_1 =
NaN NaN NaN
NaN NaN NaN
Bucket_2 =
NaN NaN NaN
NaN NaN NaN
Bucket_3 =
NaN NaN NaN
NaN NaN NaN
Bucket_4 =
NaN NaN NaN
NaN NaN NaN
another_var_1 = Hellp World
another_var_2 = Hellp World
another_var_3 = Hellp World
one_more_var_1 =
{
[1,1] =
0 0 0
0 0 0
[1,2] =
0 0 0
0 0 0
[1,3] =
0 0 0
0 0 0
}
Caveat
- Controls on the consistency of the input (e. g. the length of
dynamic_vars
and how_many
must be the same, ...) have to be added
- The code has been tested with Octave 4.2.1