hhh
hhh

Reputation: 52820

Encapsulating code with anonymous functions in Matlab?

I need to repeat this code many times. It is part of system-tester.

testFvB=@(fBE,fMCS,CI) 
{
    d='FV';
    dF=strcat('testing/systemTestFiles/D_', fBE, '_', fMCS, '_', d, '.txt');
    bepo(fBE,CI,fMCS,d,dF,oF);

    d='B';
    oF=strcat('testing/systemTestFiles/O_', fBE, '_', fMCS, '_', d, '.txt');
    bepo(fBE,CI,fMCS,d,dF,oF);
};

but

Error: File: systemTester.m Line: 3 Column: 6
The expression to the left of the equals sign is not a valid target for an
assignment.

I don't know but it looks like Matlab does not accept anonymous functions of this large size. So how to use anonymous functions to encapsulate larger codes not just things like doIt=@(x) x+1? Is the only way for the encapsulation here to create a new file?

[Update] not working, possible to make this into an execution?

test=@(fBE,fMCS)for d=1:2
    for CI=0:0.25:1
        if d==1
            d='FV';
        else
            d='B';
        end
        oF=strcat('testing/systemTestFiles/O_', fBE, '_', fMCS, '_', d, '.txt');
        bepo(fBE,CI,fMCS,d,dF,oF);
    end
end;

fBE='TestCase1 BE Evendist v2.txt';
fMCS='TestCase1 MCS.txt';
test(fBE,fMCS)

Upvotes: 0

Views: 290

Answers (1)

Amro
Amro

Reputation: 124563

Anonymous functions can only contain a single executable statement.

So in your case, just create a regular M-file function.


If you are interested, there is a series of articles on Loren Shure's blog introducing functional programming style, using anonymous functions to do non-simple tasks.

Upvotes: 3

Related Questions