3 views (last 30 days)

I would like to call arguments of function from a string or cell array. For example, this code works as I want:

function testarg()

a=1;b=2;c=3;

m='a,b,c';

eval(['myfun(',m,')']);

function myfun(a,b,c)

a

b

c

Is there another way to do this? Say, without using eval ?

Matt J
on 28 Jan 2014

function myfun(varargin)

varargin{:}

Matt J
on 31 Jan 2014

It sounds like you should be assigning your data to struct fields, rather than to individual variables.

S.a=1;

S.b=2;

S.c=3;

Now everything is conveniently bundled in S and you can pass that around to functions as a single argument.

Amit
on 28 Jan 2014

function testarg()

a=1;b=2;c=3;

abc(a,b,c)

function abc(a,b,c)

a

b

c

This wil ldo just fine.

Opportunities for recent engineering grads.

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

Start Hunting!
## 2 Comments

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/114017-is-there-the-more-elegant-way-to-do-this#comment_193066

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/114017-is-there-the-more-elegant-way-to-do-this#comment_193066

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/114017-is-there-the-more-elegant-way-to-do-this#comment_193075

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/114017-is-there-the-more-elegant-way-to-do-this#comment_193075

Sign in to comment.