How to put a sequence of text values in one function?

For example,
I have those data
set{1} = [1]
set{2} = [6,7,8]
set{3}= [10,11,12 ]
.............
set{20} = [79,80,81]
and created text value for the next
set_names = 'set{1}'
for a = 2:20
k = sprintf(',set{%d}', a);
set_names = horzcat(set_names,k)
end
Ultimatiely, I want sentence down below works
Answer = combvec(set_names)
which should be identical to this
Answer = combvec(set{1},set{2},set{3},set{4},set{5},set{6},set{7},set{8},set{9},set{10},set{11},set{12},set{13},set{14},set{15},set{16},set{17},set{18},set{19},set{20})
I want to know how to insert saved values(as text form) in function. Not only for this problem, but for other uses.

 Accepted Answer

There is no need to use "text form". Simply pass the values stored in the cell array set (which I'll call C to avoid confusion with the built-in MATLAB function set) to the function combvec as inputs. Since they are in a cell array, it's easy to pass them as a comma-separated list into combvec (or whatever function you want).
C{1} = 1;
C{2} = [6,7,8];
C{3} = [10,11,12];
C{4} = [79,80,81];
result = combvec(C{:}) % comma-separated list cell array expansion (easy)
result = 4×27
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 6 7 8 6 7 8 6 7 8 6 7 8 6 7 8 6 7 8 6 7 8 6 7 8 6 7 8 10 10 10 11 11 11 12 12 12 10 10 10 11 11 11 12 12 12 10 10 10 11 11 11 12 12 12 79 79 79 79 79 79 79 79 79 80 80 80 80 80 80 80 80 80 81 81 81 81 81 81 81 81 81
other_result = combvec(C{1},C{2},C{3},C{4}) % explicitly referencing each element of C (cumbersome and not general)
other_result = 4×27
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 6 7 8 6 7 8 6 7 8 6 7 8 6 7 8 6 7 8 6 7 8 6 7 8 6 7 8 10 10 10 11 11 11 12 12 12 10 10 10 11 11 11 12 12 12 10 10 10 11 11 11 12 12 12 79 79 79 79 79 79 79 79 79 80 80 80 80 80 80 80 80 80 81 81 81 81 81 81 81 81 81
isequal(result,other_result) % the result is the same
ans = logical
1

2 Comments

I accepted this as it answers the question (with a much better approach than the OP was attempting).

Sign in to comment.

More Answers (0)

Categories

Asked:

on 15 Apr 2019

Commented:

on 3 Jan 2024

Community Treasure Hunt

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

Start Hunting!