0

In Matlab, I have a variable length cell of values:

C={A1,...,An}

How should I pass and distribute these values into a function able to receive a variable number of arguments?

f(A1,...,An)

Ultimately if not possible, how should I modify the beginning of this function for making this work?

2 Answers 2

2

You need to convert the cell array into a comma-separated list via curly-brace indexing, that is, use C{:}.

Example with the reshape function:

>> C = {ones(3,4), 2, 2, 3};
>> y = reshape(C{:});
>> size(y) % check
ans =
     2     2     3
Sign up to request clarification or add additional context in comments.

1 Comment

I tried with (:).... i was about to write a whole change for so tricky detail!!!
1

This is exactly what 'varargin' does for you. Read in multiple variables, e.g. f(a,b,c), and store them in a cell-array.

Then you could go for one of these 'quick and dirty' processing methods:

function asdf(varargin)
  yourarguments=[varargin{:}]; % if all numerical
  yourargumentscontain=contains(varargin,'asdf'); %if strings contained
  all_processed_args=cellfun(@(x) whateveryouwant_function(x), varargin); %for older versions of matlab
end

For more here's documentation: https://ch.mathworks.com/help/matlab/matlab_prog/parse-function-inputs.html

2 Comments

This is the converse, for reading from a function, not for inputting into a function.
sorry, the answer assumes you're working with varargin. I guess this is not the case then.

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.