Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Calling a function by looping through a vector

Subject: Calling a function by looping through a vector

From: Kirk Hammett

Date: 27 Sep, 2009 04:23:00

Message: 1 of 4

Hello everybody,
I'm trying to call a function that takes a vector and does performs an operation on it.
I have multiple vectors to send it so I though of sending the vectors in a loop:

a, b and c are all vectors.

x = ['a' 'b' 'c'];

for i = 1 : length(x)
    PerformOperation(x(i))
end

instead of:

PerformOperation(a)
PerformOperation(b)
PerformOperation(c)

Could somebody tell me how to call my function by looping through a vector? The second solution seems like a hack and I'm trying to learn the best possible way to do this.

Much appreciated.

Subject: Calling a function by looping through a vector

From: dpb

Date: 27 Sep, 2009 04:32:50

Message: 2 of 4

Kirk Hammett wrote:
> Hello everybody,
> I'm trying to call a function that takes a vector and does performs an operation on it.
> I have multiple vectors to send it so I though of sending the vectors in a loop:
>
> a, b and c are all vectors.
>
> x = ['a' 'b' 'c'];
>
> for i = 1 : length(x)
> PerformOperation(x(i))
> end
>
> instead of:
>
> PerformOperation(a)
> PerformOperation(b)
> PerformOperation(c)
>
> Could somebody tell me how to call my function by looping through
> a vector? The second solution seems like a hack and I'm trying to
> learn the best possible way to do this.

x = {a b c};
for i = 1: length(x)
   yourfunc(x{i});
end

--

Subject: Calling a function by looping through a vector

From: Kirk Hammett

Date: 27 Sep, 2009 04:58:03

Message: 3 of 4

Thank you for your help!

Subject: Calling a function by looping through a vector

From: Loren Shure

Date: 28 Sep, 2009 12:38:29

Message: 4 of 4

In article <h9mpb4$b0r$1@fred.mathworks.com>, leprekhan@yahoo.com
says...
> Hello everybody,
> I'm trying to call a function that takes a vector and does performs an operation on it.
> I have multiple vectors to send it so I though of sending the vectors in a loop:
>
> a, b and c are all vectors.
>
> x = ['a' 'b' 'c'];
>
> for i = 1 : length(x)
> PerformOperation(x(i))
> end
>
> instead of:
>
> PerformOperation(a)
> PerformOperation(b)
> PerformOperation(c)
>
> Could somebody tell me how to call my function by looping through a vector? The second solution seems like a hack and I'm trying to learn the best possible way to do this.
>
> Much appreciated.
>

The function arrayfun might help.

--
Loren
http://blogs.mathworks.com/loren

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us