Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: input string used as function input
Replies: 4   Last Post: Oct 11, 2013 3:48 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Derek Goring

Posts: 3,919
Registered: 12/7/04
Re: input string used as function input
Posted: Oct 11, 2013 3:48 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On Saturday, October 12, 2013 8:04:06 AM UTC+13, Jason wrote:
> TideMan <mulgor@gmail.com> wrote in message <a9712799-9ef9-426b-921c-c2b281e93618@googlegroups.com>...
>

> > On Friday, October 11, 2013 10:28:05 AM UTC+13, Jason wrote:
>
> > > Hello,
>
> > >
>
> > >
>
> > >
>
> > > I would like to take an input string and convert it to a function name.
>
> > >
>
> > >
>
> > >
>
> > > For example I have the arrays A=[1,2],B=[2,3],C=[4,5]. I then get user input "A,B". I want to take that string and convert it to reference to the arrays A and B to put into a function that requires 2 arrays as input myFun(array1,array2). That way i can also get input "B,C" and pass in B and C into my function.
>
> >
>
> > The best way is to use a structure which has fields s.A, s.B, and s.C
>
> > Then you can get the fields as strings and refer to them like this:
>
> > myFun(s.(string1),s.(string2))
>
> > where string1 and string2 are 'A','B', or 'C'
>
> >
>
> > If someone tries to tell you that that the evil function eval is the way to go, ignore them. Use of eval inevitably results in grief.
>
>
>
> I think I'm getting closer but I cant figure out how to have the input turn into the function input. My code looks like this:
>
>
>
> d = dir('*.xlsx');
>
> file_list = {d.name}';
>
>
>
> for i=1:size(file_list)
>
> arr.(char('A'+i-1))=i;%xlsread(file_list{i});
>
> file_list{i,2}=char('A'+i-1);
>
> fprintf('Reading %s \n',file_list{i});
>
> %arr.character=xlsread(file_list{i})
>
> end
>
>
>
> file_list
>
> input('Enter Letters','s')
>
> output=myfun('y',_________)
>
>
>
>
>
> I would like to have the input be a variable size so if i have 10 excel files labelled from A->J I can put 'A,B,C,F,H' and it would use those structures as inputs to my function. So it will become myfun('y',A,B,C,F,H).


What do you expect to get from this:
char('A'+i-1)
You're mixing up characters with numerics which Matlab does not like.
Perhaps you mean:
['A' num2str(i-1)]
or is it:
char('A') + i-1



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.