Calling multiple outputs of a function into new function

I have a function with multiple outputs, now i want to call them into a new function and apply some formulas to them.
Applying formulas on each output one by one seems impossible, so any simplest way to do this?
Thanks!

5 Comments

"Applying formulas on each output one by one seems impossible"
Why?
What have you tried so far?
It is not clear what the problem is. MATLAB functions can be written to return or accept multiple arguments, so what it stopping you from doing this? If you have many variables, then have you considered putting them all into one ND numeric array or a cell array?
Sir, i have series of outputs from a function, which are matrices, lets say
out1, out2, ....., outn
And i have another function, where the formula is, my question is how can i call all the outputs and apply them the formula?
I was able to do it one by one without function, but it needs every time to update the values from out1 to outn, so i am asking for a simpler way.
It is not clear if "series of outputs from a function, which are matrices, lets say"
"out1, out2, ....., outn"
are multiple outputs from one function call, or multiple calls each returning one (or a few) outputs. Please clarify: are you calling the function once and getting multiple outputs, or calling the function multiple times? And also show us what you have tried so far.
First i made a function
[out1, out2, ....., outn] = fun(in1, in2)
but it didnt give me any output,except the first one, so i removed the function then it gave me a series of outputs (out1, out2, ....., outn) and i start to call them one by one in another function. but this method takes too long, my question is how to avoid this method and use an efficient way.
@asim: I see a general problem here. Your explanations are most likely clear, if somebody knows, what you are doing already. But the readers do not have the faintest idea.
Please do not only describe in words, what you are doing, but post the code. This will help us to understand.

Sign in to comment.

 Accepted Answer

With some bold guessing:
[out1, out2, ....., outn] = fun(in1, in2)
but it didnt give me any output,except the first one,
This might mean, that you define the function as
function [out1, out2, ....., outn] = fun(in1, in2)
...
and call it like this:
fun(in1, in2)
but catching the outputs is obligatory:
[out1, out2, ....., outn] = fun(in1, in2)
Then
i removed the function
might mean, that you removed the top line of the function definition "function [out1, out2, ....., outn] = fun(in1, in2)", such that the code becomes a script. This is a bad idea, because functions are much cleaner and safer. Nevertheless, it might work.
i start to call them one by one in another function. but this
method takes too long, my question is how to avoid this method
and use an efficient way.
I cannot guess, what "call them one by one in another function" means and without seeing the code it is impossible to recognize, why it is taking "too long". Perhaps a pre-allocation is forgotton or teh code can be vectorized, maybe expensive operations are performed repeatedly.
I do not even know what "too long" means - weeks or milliseconds?
So, please, post the code, provide some useful inputs, replace irrelevant input data by radnom arrays and give us a chance to reconsider, what you are doing. Then it will be possible to solve your specific problem.

5 Comments

Thanks @Jan for responding, you have guessed well!
Lets say i want to make a function for
out1 = rand(5, 3);
out2 = rand(5, 2);
out3 = rand(2, 5);
out4 = rand(5, 3);
out5 = rand(2, 3);
out6 = rand(3, 6);
And in another function i have a code that find outs the numbers that are greater than 0.5 from each out( out1, ...., out6), how we can do that?
function Out = MyOutCreator
Out = {rand(5, 3), rand(5, 2), rand(2, 5), rand(5, 3), ...
rand(2, 3), rand(3, 6)};
end
Then call it from another function:
function MyCounter
Out = MyOutCreator;
OutG = cell(1, numel(Out));
for iOut = 1:numel(Out)
M = Out{iOut};
OutG{iOut} = M(M > 0.5);
end
The idea is not to use a bunch of variables with numbers hidden in the name, but to create a cell array such that the elements can be processed in a loop.
Aha, this seems to be a case of the X-Y problem. Maybe we can finally start to help you. Here is one easy solution to the question you pose in your comment above:
M = [5,3;5,2;2,5;5,3;2,3;3,6]; % the matrix sizes.
R = size(M,1);
C = cell(R,1);
for k = 1:R
C{k} = rand(M(k,:)); % generate the matrices using those sizes
end
and then you just need to pass one variable C to your next function:
D = cell(size(C));
for k = 1:numel(C)
D{k} = C{k}>0.5;
end
And D is the output, a cell array of logical arrays, where each logical array shows the conditions that you test for.
Did you see how I just managed to solve your problem by using good program design, in particular I did not try to handle many individual numbered variables (which is very bad program design), but simply put all of the matrices into one cell array. And of course passing one cell array between functions is trivially easy!
Thanks @Stephen, _ Did you see how I just managed to solve your problem by using good program design,_ Thats what i want to do, but i am bit new to matlab, so im learning the things coming to my way and trying to avoid the mistakes in future. Anyway thank you guys for helping me out.

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 13 May 2017

Commented:

on 14 May 2017

Community Treasure Hunt

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

Start Hunting!