user2178841
user2178841

Reputation: 859

Accessing Functional Values in Matlab

I want to write a function that return four different variables. I wrote a smfunctionof.m file with heading

function [a,b,c,d]=smfunctionof(x)

where x is a size 20 vectorand under the body I have something like

a=x.^2;
b=x.^4;
c=x.^10;
d=x.^12;

Now, in the code script, I want access those variables. When I type smfunctionof(x), I recieve an answer (that returns, if I remember correctly, a=x.^2 or something). But when I continue and write a code something like a; or even sqrt(a), it does not return it. How can I make a variable from a function usable, or exactly, how do I use variables in function in main script? I have already included those in arguments.

Upvotes: 0

Views: 60

Answers (1)

Jonas
Jonas

Reputation: 74940

When you want to use smfunctionof in your code, you call it as [a,b,c,d] = smfunctionof(x);. This way, a value will be assigned to variables a through d. Note that you are not limited to using a through d, [u,vv,www,xxxx] = smfunctionof(x); will work just as well (though x.^2 is now assigned to a variable named u). Actually, using more explicit variable names would be even better for the readability of your code.

Upvotes: 1

Related Questions