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:
execute a string

Subject: execute a string

From: Misha Koshelev

Date: 10 May, 2009 01:52:02

Message: 1 of 3

Hi,

I don't know if this is possible, but I was wondering is there a way to execute an arbitrary string as matlab code in place? Maybe a trick...

In other words if say I pass in a function argument:

a = 'if i == 1; c = 1; end';

I'd like to somehow "run" a from my function.

Thanks
Misha

Subject: execute a string

From: Jan Simon

Date: 10 May, 2009 02:00:18

Message: 2 of 3

Dear Misha Koshelev!

> I don't know if this is possible, but I was wondering is there a way to execute an arbitrary string as matlab code in place? Maybe a trick...
> a = 'if i == 1; c = 1; end';

Use EVAL.

Kind regards, Jan

Subject: execute a string

From: Matt Fig

Date: 10 May, 2009 02:49:02

Message: 3 of 3

"Misha Koshelev" <mk144210@bcm.edu> wrote in message <gu5c02$lr6$1@fred.mathworks.com>...
> Hi,
>
> I don't know if this is possible, but I was wondering is there a way to execute an arbitrary string as matlab code in place? Maybe a trick...
>
> In other words if say I pass in a function argument:
>
> a = 'if i == 1; c = 1; end';
>
> I'd like to somehow "run" a from my function.
>
> Thanks
> Misha





Out of curiosity, why in the world would you want to do this? It seems that there must be a better way to do what you are trying to do. The facts that eval is slow and error prone should make you at least consider if there is a better way to do what you are wanting to do.

Tags for this Thread

No tags are associated with 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