From: Mathieu on
Hi,

I just cannot figure it out...

I ask for an input, which is the month I want to work with:

>input=input('Which month ?', 's')

then I want to use the string input to plot the correct variable,
for example if the input is jan (for january), I want to plot the variable called J2jan_avg

>pcolor(longitude,latitude,J2jan_avg(:,:));

My problem is that I cannot find a way to insert my string input into this line of command to plot the right variable... anyone has an idea ?

thank you for your help !
Mat
From: us on
"Mathieu " <md3g09(a)soton.ac.uk> wrote in message <i2mbqo$n6p$1(a)fred.mathworks.com>...
> Hi,
>
> I just cannot figure it out...
>
> I ask for an input, which is the month I want to work with:
>
> >input=input('Which month ?', 's')
>
> then I want to use the string input to plot the correct variable,
> for example if the input is jan (for january), I want to plot the variable called J2jan_avg
>
> >pcolor(longitude,latitude,J2jan_avg(:,:));
>
> My problem is that I cannot find a way to insert my string input into this line of command to plot the right variable... anyone has an idea ?
>
> thank you for your help !
> Mat

first: do NOT use name the output var of INPUT -> INPUT(!); otherwise, you'll obviously not be able to call this ML function, again...

one of the solutions

inp=input('month? ','s');
% assume the user entered: jan
vnam=sprintf('j2%s_avg',inp)
% vnam = j2jan_avg
% now, use VNAM in your code...

also, make sure you're not ...violating... this common-sense rule

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

us
From: Oleg Komarov on
"Mathieu " <md3g09(a)soton.ac.uk> wrote in message <i2mbqo$n6p$1(a)fred.mathworks.com>...
> Hi,
>
> I just cannot figure it out...
>
> I ask for an input, which is the month I want to work with:
>
> >input=input('Which month ?', 's')
>
> then I want to use the string input to plot the correct variable,
> for example if the input is jan (for january), I want to plot the variable called J2jan_avg
>
> >pcolor(longitude,latitude,J2jan_avg(:,:));
>
> My problem is that I cannot find a way to insert my string input into this line of command to plot the right variable... anyone has an idea ?
>
> thank you for your help !
> Mat

1) Store your variables into a structure:
s.J2jan_avg = J2jan_avg;
s.J2feb_avg = J2feb_avg;
etc...

2) Use dynamic field indexing to access those variables:
inp = input('Which month ?', 's');
inp =
'jan'

dynField = ['J2', inp, '_avg']

pcolor(longitude,latitude,s.(dynField));

Oleg
From: Mathieu on

> first: do NOT use name the output var of INPUT -> INPUT(!); otherwise, you'll obviously not be able to call this ML function, again...
>
> one of the solutions
>
> inp=input('month? ','s');
> % assume the user entered: jan
> vnam=sprintf('j2%s_avg',inp)
> % vnam = j2jan_avg
> % now, use VNAM in your code...
>
> also, make sure you're not ...violating... this common-sense rule
>
> http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
>
> us

thanks a lot for your help, but the problem kind of remains... this code creates a string variable called vnam that contains the string J2jan_avg.

The fact is that I have in my workspace:

J2jan_avg
J2feb_avg
J2mar_avg
J2apr_avg

and I want to plot the correct variable in function of the input specified by the user. and in the pcolor function, I have to enter the name of a variable that contains the data

>pcolor(longitude,latitude,J2sep_avg(:,:))

I think of something a bit like str2num but instead of changing a string in a number, it would insert a string into a variable name...
any idea ?
thanks again!
From: us on
"Mathieu " <md3g09(a)soton.ac.uk> wrote in message <i2mdmo$nvo$1(a)fred.mathworks.com>...
>
> > first: do NOT use name the output var of INPUT -> INPUT(!); otherwise, you'll obviously not be able to call this ML function, again...
> >
> > one of the solutions
> >
> > inp=input('month? ','s');
> > % assume the user entered: jan
> > vnam=sprintf('j2%s_avg',inp)
> > % vnam = j2jan_avg
> > % now, use VNAM in your code...
> >
> > also, make sure you're not ...violating... this common-sense rule
> >
> > http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
> >
> > us
>
> thanks a lot for your help, but the problem kind of remains... this code creates a string variable called vnam that contains the string J2jan_avg.
>
> The fact is that I have in my workspace:
>
> J2jan_avg
> J2feb_avg
> J2mar_avg
> J2apr_avg
>
> and I want to plot the correct variable in function of the input specified by the user. and in the pcolor function, I have to enter the name of a variable that contains the data
>
> >pcolor(longitude,latitude,J2sep_avg(:,:))
>
> I think of something a bit like str2num but instead of changing a string in a number, it would insert a string into a variable name...
> any idea ?
> thanks again!

well... YES...
now we're back to the faq entry, which i gave you:
do NOT use this approach...
look at what oleg told you above, which is a typical solution for this kind of common problems...

us