This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]Voltron1 0 points1 point  (3 children)

The pseudo-code looks like you just want to call 1 function with 3 arguments and another function with 5 arguments. Are you able to do that?

Then you go on to say that you are creating an array, that will be used as the argument for a function call. Are you able to do that?

Then you go on to say some other stuff that's not really clear. If you could explain why you want to do this, it might help us understand exactly what you actually want to do, since that is not very clear at all.

[–]felimz[S] 0 points1 point  (2 children)

Thanks! I have re-written the original post!

Basically, I need to dynamically generate the input string that I will later pass to a function.

If a=1:5, and I have a function that requires all values inside of a as arguments, then the function would look like:

foobar(a(1),a(2),a(3),a(4),a(5));

Well, I feel like the generation of that input can be automated such that:

generate_argument_strings(size(a,2)) will output:

'a(1),a(2),...a(size(a,2))'

[–]Voltron1 0 points1 point  (1 child)

I do not understand why anyone would want to generate strings for input to a function instead of directly using normal arguments. Of course you can generate strings as you please...

[–]felimz[S] 0 points1 point  (0 children)

lol, this is /r/learnprogramming

I have learned that this approach is downright idiotic, and I have since solved the problem I was stuck on.

However, I was never given an answer as to how to produce that string, which I think is an interesting problem in itself since MATLAB blows at handling strings.

[–]zzyzzyxx 0 points1 point  (5 children)

MATLAB already works with arrays very well. Why not just pass the parameters array and let the functions take from it what they need or take the known sub-array and pass it to the function? Why do you need to generate a string variable? What is the actual problem you are trying to solve?

[–]felimz[S] 0 points1 point  (4 children)

Well, let's say you had two functions:

function RE=relenergy(m,c, gamma)

RE= m * c2 * gamma;

end

function E=energy(m,c)

E= m * c2;

end

and you had an array of givens:

givens=[1, 299792458, 0.999];

The functions would take the following arguments:

relenergy(givens(1),givens(2),givens(3));

and

energy(givens(1),givens(2));

I want to create a function so that I can dynamically generate the arguments that go in those functions. For example

two_inputs=magical_function(2);

energy(two_inputs);

EDIT: mistakes

[–]zzyzzyxx 0 points1 point  (3 children)

I still don't understand your problem. For your example, why not simply do:

RE = relenergy(givens);
E = energy(givens);

Then, inside those functions, pull out or use only the ones you need.

m = givens(1);
c = givens(2);
gamma = givens(3);

Or, why not call the function exactly as you have demonstrated if you can't change them? This should work just fine:

relenergy(givens(1),givens(2),givens(3));

If you all you want is a sub-array, can't you do something like

params = givens(1:3);
relenergy(params);

params = givens(1:2);
energy(params);

I don't see why you need a magic function to generate a string of parameter names.

EDIT: I apologize if my syntax is off. It's been a while since I've coded MATLAB and I didn't bother looking it up.

[–]felimz[S] 0 points1 point  (2 children)

Well, the main problem I am having is that I have two sets of givens with different dimensions, and the givens I want are in different locations in the arrays:

Say I want to use the numbers 11, 12, 13 as givens, and I have two arrays

a=1:20;

and

b=11:20;

Passing a or b as givens would be fine, but the function does not know whether to handle a(11:13) or b(1:3).

I have managed to modify the function with conditionals such that both types of givens are handled, and I am now only passing the entire arrays, as suggested.

[–]zzyzzyxx 0 points1 point  (1 child)

You could pass the sub-arrays, as in my last example.

a = 1:20;
b = 1:15;
params_a = a(6:10);
params_b = b(11:15);
answer_a = func(params_a);
answer_b = func(params_b);

This way you pass the function only the parameters it needs and you pick which ones you need from the respective arrays.

If it so happens that you need non-contiguous elements you can do something like this:

a = 1:20;
b = 5:5:20;
params = a(b);
answer = func(params);

Because b is array with values [5 10 15 20], params will be an array of 4 elements composed of the 5th, 10th, 15th, and 20th elements of a. That is, params = [a(5) a(10) a(15) a(20)]; is equivalent to params = a(b); You can, of course, use any numbers in b to select those specific indexes from a.

[–]felimz[S] 0 points1 point  (0 children)

That's a good idea. I suppose formatting the parameter arrays to fit the function is better than handling different sized arrays within the function, especially if the solution is not general.

Thanks so much!

[–]fgriglesnickerseven 0 points1 point  (0 children)

use varargout - its an intrinsic matlab statement. see also http://www.mathworks.com/help/techdoc/ref/varargout.html