MATLAB: Error in a user-define function calling.

function calling

Hi, i am returning multiple values from a user-function but this gives an error of Too many output arguments , i don't know where i am making the mistake ?
This is my function calling inside the loop,
for i = 1:no_users
[indexes(:,i), users_data(i,:)] = singleuser_traffic(num_event);
end
And this is the first line of the user-function
function [index, sort_user1] = singleuser_traffic(num_event)
I can't see any mistake in my function call, Thank you so much

Best Answer

Providing you’re not using the ‘index’ and ‘sort_user1’ variables elsewhere, try:
for i = 1:no_users
[index, sort_user1] = singleuser_traffic(num_event);
indexes(:,i) = index;
users_data(i,:) = sort_user1;
end
This doesn’t significantly add to the overload, and will let you see what the incompatibility is between what the function returns and what you want in your ‘indices’ and ‘users_data’ arrays.
I’ve found that for whatever reason, most functions will crash if you ask them to fill arrays for you. Let them return what they want, and do the array assignments just after.
BTW, num_event isn’t subscripted in your loop, so unless you’re defining it to change outside the loop you posted, you’re giving it the same argument in each iteration.