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:
Combining printed strings into working functions

Subject: Combining printed strings into working functions

From: Nicolas

Date: 16 Apr, 2010 03:13:05

Message: 1 of 5

I hope I can explain my issue well enough....

So, I'm trying to work around some functions that are strict in their input arguments. This particular function is in a loop, and the argument needs to change with each iteration of the loop, but I can't figure out how (if possible) to input a variable as the argument. So instead, I'm trying to couple some strings together and then run the result.

For example:
coolfunc(height, weight)

Let's say that I would like weight to increase as I get further along in the loop. How can I do this? I've been trying to set up something like:

for weight=1:10
x = sprintf('coolfunc(height, %d)', weight)
end

is there any way to turn this sprint output into a running function?

I hope that made some kind of sense!

Subject: Combining printed strings into working functions

From: kinor

Date: 16 Apr, 2010 07:41:05

Message: 2 of 5

"Nicolas " <nickbedo@yahoo.com> wrote in message <hq8kk1$r8a$1@fred.mathworks.com>...
> I hope I can explain my issue well enough....
>
> So, I'm trying to work around some functions that are strict in their input arguments. This particular function is in a loop, and the argument needs to change with each iteration of the loop, but I can't figure out how (if possible) to input a variable as the argument. So instead, I'm trying to couple some strings together and then run the result.
>
> For example:
> coolfunc(height, weight)
>
> Let's say that I would like weight to increase as I get further along in the loop. How can I do this? I've been trying to set up something like:
>
> for weight=1:10
> x = sprintf('coolfunc(height, %d)', weight)
> end
>
> is there any way to turn this sprint output into a running function?
>
> I hope that made some kind of sense!

help eval

Subject: Combining printed strings into working functions

From: nanren888

Date: 16 Apr, 2010 07:58:06

Message: 3 of 5

"Nicolas " <nickbedo@yahoo.com> wrote in message <hq8kk1$r8a$1@fred.mathworks.com>...
> I hope I can explain my issue well enough....
>
> So, I'm trying to work around some functions that are strict in their input arguments. This particular function is in a loop, and the argument needs to change with each iteration of the loop, but I can't figure out how (if possible) to input a variable as the argument. So instead, I'm trying to couple some strings together and then run the result.
>
> For example:
> coolfunc(height, weight)
>
> Let's say that I would like weight to increase as I get further along in the loop. How can I do this? I've been trying to set up something like:
>
> for weight=1:10
> x = sprintf('coolfunc(height, %d)', weight)
> end
>
> is there any way to turn this sprint output into a running function?
>
> I hope that made some kind of sense!

Nicolas,
I have foundthe following to be mostly true
(1) Eval can do anything
(2) Eval should not be allowed to do anything.

> I hope I can explain my issue well enough....
Nope. I have no idea why you would need eval & what you are trying to achieve.

height = 7;
for weight=1:10
x = simpleFunc(height, weight);
end

appears to do what you want.
(1) weight becomes each or 1,2,3,4,5,6,7,8,9 & 10
one after another.
(2) the function is called for each value
(3) The values of height & weight wil be passed to simpleFunc when called.
If this has not helped, consider asking with different wording.
I have used eval, many times in the past, but more recent Matlab language enhancements mean that I have not needed it for a while, & then only for weird stuff like temporarily trying to debug code.

Subject: Combining printed strings into working functions

From: Steven Lord

Date: 16 Apr, 2010 13:56:51

Message: 4 of 5


"Nicolas " <nickbedo@yahoo.com> wrote in message
news:hq8kk1$r8a$1@fred.mathworks.com...
>I hope I can explain my issue well enough....
>
> So, I'm trying to work around some functions that are strict in their
> input arguments. This particular function is in a loop, and the argument
> needs to change with each iteration of the loop, but I can't figure out
> how (if possible) to input a variable as the argument. So instead, I'm
> trying to couple some strings together and then run the result.
>
> For example:
> coolfunc(height, weight)
>
> Let's say that I would like weight to increase as I get further along in
> the loop. How can I do this? I've been trying to set up something like:
>
> for weight=1:10
> x = sprintf('coolfunc(height, %d)', weight)
> end
>
> is there any way to turn this sprint output into a running function?

Why don't you just call the function directly? Why go through SPRINTF?

height = 17;
for weight = 1:10
    x = coolfunc(height, weight)
end

If you want to store all the results of the function for all the weight
values, then how you'd do that depends on the sizes and shapes of the
output. As an example, if coolfunc returns a scalar (a 1-by-1 matrix, just
one number) then:

height = 17;
weight = [2 23 18.75 92];
x = zeros(1, numel(weight)); % Preallocation
for whichweight = 1:numel(weight)
    x(1, whichweight) = coolfunc(height, weight(whichweight));
end

You can do the same sort of thing with multidimensional arrays if coolfunc
returns, say, a matrix but always the same size matrix.

If coolfunc can return different sizes of outputs depending on its inputs,
use a cell array.

height = 17;
weight = [2 23 18.75 92];
x = cell(1, numel(weight)); % Preallocation
for whichweight = 1:numel(weight)
    x{1, whichweight} = coolfunc(height, weight(whichweight));
end

When you want to extract one of the outputs from x, use (for example) x{3}
to extract the output of the third coolfunc call.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Combining printed strings into working functions

From: Steven Lord

Date: 16 Apr, 2010 13:59:01

Message: 5 of 5


"nanren888 " <nanren888.remove.this@hotmail.com> wrote in message
news:hq95ae$s2d$1@fred.mathworks.com...

*snip*

> I have used eval, many times in the past, but more recent Matlab language
> enhancements mean that I have not needed it for a while, & then only for
> weird stuff like temporarily trying to debug code.

Can you say a little bit more about the "weird stuff" (either here or in
another thread) about how you've used EVAL to debug your code? I'm curious
to see if there's a more efficient way to do what you want (although if
you're running this while in debug mode, I guess efficiency isn't your
primary concern right then.)

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

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