0

So I would like to optimize my code such that I can look through an array such as

{'one','two','three'} 

and create corresponding variables defined as tables or arrays such as

one = table()
two = table()
three = table()

I am aware of the eval function however I would like to use this function in a loop s.t I allocate values to the new variable right after i create it

1 Answer 1

1

If I am understanding your question properly, given a cell array consisting only of strings, you wish to create variables in your workspace where each variable is declared as a string using the names from this cell array.

You could use eval, but I'm going to recommend something other than eval. eval should be avoided and instead of iterating those reasons, you can read Loren Shure's article on eval.

In any case, I would recommend you place these variables as dynamic fields inside a structure instead. Do something like this:

s = struct()
strs = {'one', 'two', 'three'};
for idx = 1 : numel(strs)
    s.(strs{idx}) = table();
end

In this case, s would be a structure, and you can access the variable by the dot operator. In this case, you can access the corresponding variables by:

d = s.one; %// or
d2 = s.two; %// or
d3 = s.three;

If you want to place this into a function per se, you can place this into a function like so:

function [s] = createVariables(strs)
s = struct();
for idx = 1 : numel(strs)
    s.(strs{idx}) = table();
end

This function will take in a cell array of strings, and outputs a structure that contains fields that correspond to the cell array of strings you put in. As such, you'd call it like so:

strs = {'one', 'two', 'three'};
s = createVariables(strs);

However, if you really really... I mean really... want to use eval, you can create your workspace variables like so:

strs = {'one', 'two', 'three'};
for idx = 1 : numel(strs)
    eval([strs{idx} ' = table();']);
end

To place this into a function, do:

function [] = createVariables(strs)
for idx = 1 : numel(strs)
    eval([strs{idx} ' = table();']);
end

However, be warned that if you run the function above, these variables will only be defined in the scope that the function was run in. You will not see these variables when the function exits. If you want to run a function so that the variables get defined in the workspace after you run the function, then eval is not the right solution for you. You should thus stick with the dynamic field approach that I talked about at the beginning of this post.

In any case, this will create one, two and three as workspace variables that are initialized to empty tables. However, I will argue with you that the first line of code is easier to read than the second piece of code, which is one of the main arguing points as to why eval should be avoided. If you stare at the second piece of code long enough, then you can certainly see what we're trying to achieve, but if you read the first piece of code, you can ascertain its purpose more clearly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.