putting a sequence of text values into a multivariable function.

1 view (last 30 days)
The problem is easy, but i dont find the solution.
Lets say we want combine some vectors: a1, a2, a3.
a1 = [1,2,3];
a2 = [21,22,23];
a3 = [31,32];
Comb = combvec(a1,a2,a3);
for that we can use the function Comb = combvec(a1,a2,a3). So the solution is:
a =
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
21 21 21 22 22 22 23 23 23 21 21 21 22 22 22 23 23 23
31 31 31 31 31 31 31 31 31 32 32 32 32 32 32 32 32 32
which is the vector of all the posible combinations.
Now, lets supouse that there is a case where we dont know how many vectors we have ( they are generated while running ).
However, we can get a char with the name of those vectors like:
n = 3;
final=[];
for k=1:3
if n >= 2
Data = ['a' num2str(k) ','];
n=n-1;
else
Data = ['a' num2str(k)];
end
final=strcat(final,Data);
end
Comb = combvec(final).
And the result of this is operation is a single char: final = ('a1,a2,a3');
But if we try to insert this array into the function like: Comb = combvec(final). We will get this result:
a =
'a1,a2,a3'
Can someone help me to solve it?
Thanks

Answers (1)

Shanmukha Voggu
Shanmukha Voggu on 1 Dec 2021
Hi Jamie,
I understood that you want to process a char that contains the variable names of matrices separated by commas
This can be achieved by calling combvec multiple times
a1 = [1,2,3];
a2 = [21,22,23];
a3 = [31,32];
% The code below assumes the variable names mentioned in "inputString"
% are already inside the workspace
inputString='a1,a2,a3';
matrixNames=split(inputString,",")% split the string using delimiter as comma
matrixNames = 3×1 cell array
{'a1'} {'a2'} {'a3'}
answer=eval(matrixNames{1});% initialize the answer variable
for i = 2:length(matrixNames)
secondMatrix=eval(matrixNames{i});% evaluate the expression
answer=combvec(answer,secondMatrix);
end
answer
answer = 3×18
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 21 21 21 22 22 22 23 23 23 21 21 21 22 22 22 23 23 23 31 31 31 31 31 31 31 31 31 32 32 32 32 32 32 32 32 32
Refer this for more information.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!