From: Enrique Gil on
Hi,

I have a question regarding the possbility of writing a for loop of something of the sort that would scroll through a list of variables. For example, I have 10 different variables, ie. AM_1, AM_2, AM_3,...etc. I need to do apply a certain algorithm to each of the variables, the algorithm being essentially a series of for and if loops. Right now, I have the algorithm written ten times, one for each variable name. So my question is, how can I write a for loop that scrolls through the variables and then have the algorithm nested within this for loop? I was thinking of having an additional variable that contains the names of the ten variables, and having the for loop read this string vector and apply the algorithm for each entry in the array. But i dont know how to read and scroll through an array like this while changing the variable being called. Can anybody help?
From: dpb on
Enrique Gil wrote:
> Hi,
>
> I have a question regarding the possbility of writing a for loop of
> something of the sort that would scroll through a list of variables.
> For example, I have 10 different variables, ie. AM_1, AM_2,
> AM_3,...etc. I need to do apply a certain algorithm to each of the
> variables, ...

<http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F>

--
From: Andy on
"Enrique Gil" <gile(a)msoe.edu> wrote in message <hvvtk5$73h$1(a)fred.mathworks.com>...
> Hi,
>
> I have a question regarding the possbility of writing a for loop of something of the sort that would scroll through a list of variables. For example, I have 10 different variables, ie. AM_1, AM_2, AM_3,...etc. I need to do apply a certain algorithm to each of the variables, the algorithm being essentially a series of for and if loops. Right now, I have the algorithm written ten times, one for each variable name. So my question is, how can I write a for loop that scrolls through the variables and then have the algorithm nested within this for loop? I was thinking of having an additional variable that contains the names of the ten variables, and having the for loop read this string vector and apply the algorithm for each entry in the array. But i dont know how to read and scroll through an array like this while changing the variable being called. Can anybody help?

Why not turn your algorithm into its own function, taking the variables AM_n in as inputs? Then you just need to call it for each input, which you could do in a loop if you wanted, rather than type out the whole algorithm again.
From: Andy on
While you're at it, instead of having ten different variables, have one array of variables AM(1), AM(2), etc. Write your algorithm as

function output = myalgorithm(AMn)
....
end

Then:

for ix = 1:10
output(ix) = myalgorithm(AM(ix));
end
From: Enrique Gil on
"Andy " <theorigamist(a)gmail.com> wrote in message <hvvv30$eb0$1(a)fred.mathworks.com>...
> While you're at it, instead of having ten different variables, have one array of variables AM(1), AM(2), etc. Write your algorithm as
>
> function output = myalgorithm(AMn)
> ...
> end
>
> Then:
>
> for ix = 1:10
> output(ix) = myalgorithm(AM(ix));
> end


Hey Andy,

Thanks for your help. I am actually still struggling a bit with the syntax. Let me give you a simple example from my code and see how you would implement this. There are much much larger algorithms that I must apply, but for simplicty I'll just use applying a filter. First of all I need to load .mat file which contains all the variables. Each of these variables contains a different sized matrix. I must then design my low-pass filter (already designed) and apply it to each of the variables. However, I need to also create a new variable which contains the filtered version for each of the original variables. For instance, the original is AM_1 and I need to create AM_filt_1. Note that this is just one of the "algorithms" within my m-file and a few other ones must be applied afterwards without user input such as interpolations, curve fittings, etc. I guess, I'm just not sure where
to place the algorithms given I use a function as you suggested before.

Thank you so much for your help!