1

I am new to matlab. I have the following code like this:

names=arrayfun(@num2str, 1:nseq_all, 'unif', 0);

After searching, I understand that arrayfun applies a function to each element of an array. So, I guess in this case, we apply num2str function to each element of the array 1:nseq_all. 'unif' and 0 are the arguments for the function and the corresponding value part. I have trouble to understand this part. Any comments are greatly appreciated.

2 Answers 2

3

'unif',0 is shorthand for 'UniformOutput',false, which means that the output does not have the same dimensions as the input array 1:nseq_all.

This is because a string 1 dimensions 1x1, but 124 has dimensions 1x3.

names will be a cell array, as a normal numeric array can't contain rows/columns with different numbers of elements.

Be sure to carefully read the arrayfun documentation.

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

Comments

1

'unif', 0 is shorthand for 'UniformOutput',false. Note that parameterName/parameterValue pairs in Matlab allow the abbreviation of the parameterName, as long as it doesn't conflict with another possible parameter. In this case 'un',0 would have worked as well. Anyway, what is that option for?

modifiedArray = arrayfun(function_handle, array)

applies the function defined in function_handle to each element of array and returns modifiedArray, which is the same size as array, and the class of whatever function_handle returns. This syntax can only be used if the output of function_handle is scalar, though class doesn't matter, so the output of function_handle can be a scalar structure, i.e. arrayfun(@(x)struct('field',x),magic(4)) is valid.

cellArray = arrayfun(function_handle, array, 'UniformOutput', false)

applies the function defined in function_handle to each element of array and returns cellArray, which is the same size as array. Each element of cellArray contains the output of a call of function_handle. This syntax must be used if the output of function_handle is non-scalar (even if each calculation returns a 1-by-2 array, which is totally uniform), but of course, you can use it with scalar output as well.

In your case, num2str returns a character array which is non-scalar if the argument of num2str goes above 9. Consequently, you need to set UniformOutput to false.

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.