yusuf
yusuf

Reputation: 3781

getting a return value from a function in MATLAB

I have such a matlab function:

function j = globalfun(a, xr, x)

        gv_0 = 0;
        gv_1 = 0;

        counter1_0 = 0;
        counter1_1 = 0;
        counter2_0 = 0;
        counter2_1 = 0;
        counter3_0 = 0;
        counter3_1 = 0;
        counter4_0 = 0;
        counter4_1 = 0;
        ............................................
        ............................................
        score = gv_0/gv_1;
end

I haven't written all the function codes, because it is not needed.

The question is, I need to get "score" value from another script I'm using.

How can I manage the issue?

Thanks,

Upvotes: 0

Views: 80

Answers (1)

Sébastien Dawans
Sébastien Dawans

Reputation: 4626

I assume you copy-pasted the function syntax from somewhere and that you don't need to return j. Define this function in globalfun.m

function score = globalfun(a, xr, x)
    ...
    score = gv_0/gv_1;
end

The value of score is assigned within globalfun and will be set as return value. Then call it from a script or another function

myscore = globalfun(a, xr, x)

If you need multiple return values, use square brackets

function [i,j,score] = globalfun(a, xr, x)
    ...
    i = ...
    j = ...
    score = gv_0/gv_1;
end

Upvotes: 2

Related Questions