evaluating strings

Hello,

I would really appreciate any help with the problem I am currently
experiencing with the eval function.

s1='myif(plus(1,2),times(3,4))';
eval(s1);

When I do eval(s1) it executes plus(1,2) then times(3,4) then myif.
This is not acceptable since both plus and times are executed when
only one should be.

I have tried using function handles like this:

s2=@myif(@plus(1,2),@times(3,4));
eval(s2);

This works better however MATLAB executes either plus(1,2) or
times(3,4) and then myif. It executes the nested function first which
is also unacceptable.

I know this works perfectly:

s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')';
eval(s3);

It executes myif and then either plus(1,2) or times(3,4), depending
on the myif test.

So, what I would like to know is this, is there a way to go from s1
to s3 automatically?

s1='myif(plus(1,2),times(3,4))';
s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')';

I have tried to write a program using the brackets to indicate how
many quotation marks to wrap each function in but it is a very messy
solution. I have used strtok and strcat etc. but I have problems when
several brackets (delimiters) are touching each another.

If anyone out there can think of a solution to my problem (or the
larger eval issue) I would be extremely grateful.

Thank you so much in advance for your help.

Matt.

free_wombat@hotmail.com
0
7/20/2006 12:40:59 AM
comp.soft-sys.matlab 206924 articles. 560 followers. lunamoonmoon (258) is leader. Post Follow

2 Replies
124 Views

Similar Articles

[PageSpeed] 31
In article <ef3c24f.-1@webcrossing.raydaftYaTP>,
 "Matt C" <free_wombat@hotmail.com> wrote:

> Hello,
> 
> I would really appreciate any help with the problem I am currently
> experiencing with the eval function.
> 
> s1='myif(plus(1,2),times(3,4))';
> eval(s1);
> 
> When I do eval(s1) it executes plus(1,2) then times(3,4) then myif.
> This is not acceptable since both plus and times are executed when
> only one should be.
> 
> I have tried using function handles like this:
> 
> s2=@myif(@plus(1,2),@times(3,4));
> eval(s2);
> 
> This works better however MATLAB executes either plus(1,2) or
> times(3,4) and then myif. It executes the nested function first which
> is also unacceptable.
> 
> I know this works perfectly:
> 
> s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')';
> eval(s3);
> 
> It executes myif and then either plus(1,2) or times(3,4), depending
> on the myif test.
> 
> So, what I would like to know is this, is there a way to go from s1
> to s3 automatically?
> 
> s1='myif(plus(1,2),times(3,4))';
> s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')';
> 
> I have tried to write a program using the brackets to indicate how
> many quotation marks to wrap each function in but it is a very messy
> solution. I have used strtok and strcat etc. but I have problems when
> several brackets (delimiters) are touching each another.
> 
> If anyone out there can think of a solution to my problem (or the
> larger eval issue) I would be extremely grateful.
> 
> Thank you so much in advance for your help.
> 
> Matt.
> 
> free_wombat@hotmail.com

This is one of the most bizarre questions I have seen.  Congratulations.

I have no idea what you're trying to do, but here are some observations.

1)  We have no idea what the function myif is.  Perhaps we don't need to 
know, but knowing helps put the question into context.

2)  How do you know when only one or the other argument is evaluated?

3)  s3 evaluates to

  >> s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')' 
  s3 =
  myif('plus('1','2')','times('3','4')')


  plus('1','2') is not at all the same as plus(1,2).

4)  In the s1 example you seem surprised that both arguments are 
evaluated before they are passed into myif, but no other behavior is 
possible.  In your other examples you apparently defer execution by 
passing in a string instead of a value.  Why, I don't know.

5)  Since I don't know what myif is let's pretend that it's

  if A || B
    ...
  end

MATLAB uses "short_circuit" operators in this case so if you just wrote

  if plus(1,2) || times(3,4)

you should get the behavior you desire.

-- 
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.
0
see9548 (1248)
7/20/2006 1:05:26 AM
Doug Schwarz wrote:
>
>
> In article <ef3c24f.-1@webcrossing.raydaftYaTP>,
> "Matt C" <free_wombat@hotmail.com> wrote:
>
>> Hello,
>>
>> I would really appreciate any help with the problem I am
> currently
>> experiencing with the eval function.
>>
>> s1='myif(plus(1,2),times(3,4))';
>> eval(s1);
>>
>> When I do eval(s1) it executes plus(1,2) then times(3,4) then
> myif.
>> This is not acceptable since both plus and times are executed
> when
>> only one should be.
>>
>> I have tried using function handles like this:
>>
>> s2=@myif(@plus(1,2),@times(3,4));
>> eval(s2);
>>
>> This works better however MATLAB executes either plus(1,2) or
>> times(3,4) and then myif. It executes the nested function first
> which
>> is also unacceptable.
>>
>> I know this works perfectly:
>>
>> s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')';
>> eval(s3);
>>
>> It executes myif and then either plus(1,2) or times(3,4),
> depending
>> on the myif test.
>>
>> So, what I would like to know is this, is there a way to go
from
> s1
>> to s3 automatically?
>>
>> s1='myif(plus(1,2),times(3,4))';
>> s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')';
>>
>> I have tried to write a program using the brackets to indicate
> how
>> many quotation marks to wrap each function in but it is a very
> messy
>> solution. I have used strtok and strcat etc. but I have
problems
> when
>> several brackets (delimiters) are touching each another.
>>
>> If anyone out there can think of a solution to my problem (or
the
>> larger eval issue) I would be extremely grateful.
>>
>> Thank you so much in advance for your help.
>>
>> Matt.
>>
>> free_wombat@hotmail.com
>
> This is one of the most bizarre questions I have seen.
> Congratulations.
>
> I have no idea what you're trying to do, but here are some
> observations.
>
> 1) We have no idea what the function myif is. Perhaps we don't
> need to
> know, but knowing helps put the question into context.
>
> 2) How do you know when only one or the other argument is
> evaluated?
>
> 3) s3 evaluates to
>
> >> s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')'
> s3 =
> myif('plus('1','2')','times('3','4')')
>
>
> plus('1','2') is not at all the same as plus(1,2).
>
> 4) In the s1 example you seem surprised that both arguments are
> evaluated before they are passed into myif, but no other behavior
> is
> possible. In your other examples you apparently defer execution by
>
> passing in a string instead of a value. Why, I don't know.
>
> 5) Since I don't know what myif is let's pretend that it's
>
> if A || B
> ...
> end
>
> MATLAB uses "short_circuit" operators in this case so if you just
> wrote
>
> if plus(1,2) || times(3,4)
>
> you should get the behavior you desire.
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.
>
  

Hello,

I would really appreciate any help with the problem I am currently
experiencing with the eval function.

s1='myif(plus(1,2),times(3,4))';
eval(s1);

When I do eval(s1) it executes plus(1,2) then times(3,4) then myif.
This is not acceptable since both plus and times are executed when
only one should be.

I have tried using function handles like this:

s2=@myif(@plus(1,2),@times(3,4));
eval(s2);

This works better however MATLAB executes either plus(1,2) or
times(3,4) and then myif. It executes the nested function first which
is also unacceptable.

I know this works perfectly:

s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')';
eval(s3);

It executes myif and then either plus(1,2) or times(3,4), depending
on the myif test.

So, what I would like to know is this, is there a way to go from s1
to s3 automatically?

s1='myif(plus(1,2),times(3,4))';
s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')';

I have tried to write a program using the brackets to indicate how
many quotation marks to wrap each function in but it is a very messy
solution. I have used strtok and strcat etc. but I have problems when
several brackets (delimiters) are touching each another.

If anyone out there can think of a solution to my problem (or the
larger eval issue) I would be extremely grateful.

Thank you so much in advance for your help.

Matt.

Thank you very much to those people who have looked at my problem and
especially to Doug Schwarz for replying.

I apologise if the way I originally presented my problem was not
clear. I guess the short version was too simplified so here�s the
long one. Although I do admit to making one error in my first post in
that s3 should have been written as

'myif(''plus(''''1'''',''''2'''')'',''times(''''3'''', ''''4'''')'')'


rather than

'myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')'

as Doug realised.

This question has to do with the MATLAB genetic programming toolbox
written by Sara Silva which is available open source from <http://gplab.sourceforge.net/index.html>.



I believe it currently has one major unresolved problem and it
involves the eval function.

The GP program generates a population of random programs
(�individuals�) to solve problems. These generated programs are
represented as �trees� which are stored in nested structs. The
algorithm needs to evaluate each program to determine which ones work
the best.

Currently, programs are evaluated by first converting the tree struct
into a single layered string. In the artificial ant problem for
example, a program might look like this

string = 'antif(antif(antmove,antright),antleft)'

GPLAB then simply does eval(string) to evaluate the programs.
However, this does not work correctly (see my example programs at the
end).

About a year ago some French researchers using GPLAB wrote a program
to convert the tree structure into the strings with levels which
GPLAB could then evaluate correctly like this:

string = ' antif(''antif(''''antmove'''', ''''antright'''') '',
''antleft'') '

Unfortunately the toolbox is very large and making this change
affects many other programs.

It would be much simpler to have a new program that just converts
this

string = 'antif(antif(antmove,antright),antleft)'

to this

string = ' antif(''antif(''''antmove'''', ''''antright'''') '',
''antleft'') '

whenever an individual needs to be evaluated.

So there you have it. If anyone can think of an easy way to do this,
that would be great.

If you want to see how the eval function (mis)behaves then try out
each case of the following evalif program.

Thanks.

%evalif
 
clear all
clc
 
global x switchvar
x=0;
 
fprintf('\neval if')
fprintf('\n-----------------------\n')
 
switchvar='1';
 
switch switchvar
  
    case '1' % does NOT evaluate correctly
        iffunction(acttrue,actfalse);
    
    case '2' % does NOT evaluate correctly
        str='iffunction(acttrue,actfalse)';
        ans=eval(str);
        
    case '3' % does evaluate correctly
        iffunction(@acttrue,@actfalse);
        
    case '4' % but this does NOT evaluate correctly
        iffunction(iffunction(@acttrue,@actfalse),@actfalse);
        
    case '5' % does evaluate correctly
        str='iffunction(''acttrue'',''actfalse'')';
        ans=eval(str);
   
end
 
fprintf('\n-----------------------\n')
fprintf('\nfinal result: x = %d',x)
fprintf('\ncorrect result: x = 1\n')
 

function x=iffunction(a,b)
 
global x switchvar
 
fprintf('\nif function\n')
 
switch switchvar
    
    case '1' % if passing one string
        if 1
            a;
        else
            b;
        end
        
    case '2' % if calling functions directly
        if 1
            a;
        else
            b;
        end
        
    case '3' % if passing function handles
        if 1
            feval(a);
        else
            feval(b);
        end
        
    case '4' % if passing function handles
        if 1
            feval(a);
        else
            feval(b);
        end
        
    case '5' % if passing multiple strings
        if 1
            ans=eval(a);
        else
            ans=eval(b);
        end
 
end

function x=action1
 
global x
 
x=x+1;
 
fprintf('\nact true: x = %d\n',x)
 

function x=action2
 
global x
 
x=x+2;
 
fprintf('\nact false: x = %d\n',x)
0
7/20/2006 3:09:50 AM
Reply:
Similar Artilces:

extract numbers from string
i have a data file with a header that has several lines, which i read with readf,1,firstline readf,1,secondline ..... firstline is a string that looks like xpixels = 1024 i am trying to convert this to a variable called nx which has the value 1024. is there a clean and easy way to do this? Thanks! Tobi wrote: > i have a data file with a header that has several lines, which i read > with > > readf,1,firstline > readf,1,secondline > .... > > firstline is a string that looks like > xpixels = 1024 > > i am trying to convert this to a variable called nx wh...

Fitness Evaluation or Function Evaluation?
I'am a little embarressed to ask this question but here goes :) Now the way i've seen it describe or i understand it, a fitness evaluation for a genetic algorithm, usually used as a measure for performance is basically the number of calls to evaluate an indivduals fitness throughout the cycle of the GA, for example calls during intialisation of the a popualtion of a 1000 individuals and workign out there fitness is 1000 fitness evaluations. For a truncation selection producing 1000 new offspring each GA cycle i.e replace all is another 1000 fitness evaluations. This goes on until co...

evaluating generic inline functions
Hello, Say I have an inline function 'x1^2+x1*x2+x2^2' which I would like to evaluate at N values from an Nx2 matrix (the columns of the matrix are x1 and x2, respectively). If the inline function is fixed, there is no problem in providing the input values for the calculation of the output of the function itself. I am trying to generalize this process to any inline function, i.e. I would like to be able to define the inline function as an input to a function e.g. Y=MYFUNCTION ('NAMEOFINLINEFUNCTION',datamatrix), and would like Matlab to be able to recognize which columns...

how to enter rows of string into one row ??
hello I am working on IPTV traffic . I have the following data a=['9A 24 08 15 E2 FB 45 41 C2 66 00 CB C5 F7 61 B3 36 20 16 A6 EE 6C F1 7C 69 93 B3 36 1B 22 17 E2 F9 B1 70 DE 5B B5 CD CD 83 68 6B 8B E6 C1 A6 5B B0 6B DE 6E 34 C9 D9 E2 F9 B0 D9 8D 32 54 CB C5 F5 4C 95 32 D5 32 C4 71 7C 27 DD 7C 24 60 69 C7 C6 69 A0 42 22 03 4C 10 C8 02 26 28 69 34 97 20 9F 11 A2 DB DF 96 12 EA 8F C7 FE 16 08 EC 0B 3F 19 2F 2F 7B 85 99 66 81 8B CB C5 31 07 96 62 9C 53 DA E2 A0 89 EF CD 8B 67 AA 16 1F 80 50 BB 2F 19 66 33 3B 0A 1E 60 A8 E0 B8 1A D7 30 00 06 66 A7 8C 8A 62 E7 71 9C 3...

help parsing strings
in MS VC++ Express I need to know how to get from one comma delimited text string to many strings. from this: main_string = "onE,Two,Three , fouR,five, six " to these: string1 = "one" string2 = "two" string3 = "three" string4 = "four" string5 = "five" string6 = "six" the white space needs to be removed and the case needs to be all upper or lower. The result needs to be strings and not pointers or addresses in memory so I can test: I have it working but I have pointers for strings and can't convert them back to...

String replacement script
Hi all, I have the following strings in a file: Line 1: 10.34,6.78,9.45,9.19,ABB,ABB Grain Ltd,9.25,-15,"3,473", 9.19,9.25,10,f,0.49,3.63,1.08,4.9,188.8 Line 2: 1.23,0.86,1.08,1.04,AAH,Arana Therapeutics L, 1.06,-2,806,1.06,1.07,-,,-,0.76,-,78.17,1.4 The difference between both lines being that field 9 in line 1 is encapsulated in inverted commas, whereas in line 2 it isn't. I need a command which will remove the comma in field 9 and also remove the inverted commas, so line one now appears as: 10.34,6.78,9.45,9.19,ABB,ABB Grain Ltd,9.25,-15,3473,9.19,9.25,10,f, 0.49,3.63,1.08...

Read value and string from file
Hi all, &nbsp; I have seen some post on this question but I have a bit of confusion, My task is, I want to read some values and corresponding string from the file Lets say, &nbsp; 2.2&nbsp;&nbsp; 4.3&nbsp;&nbsp;&nbsp; Red 3.4&nbsp;&nbsp; 2.1&nbsp;&nbsp;&nbsp; Green 1.1&nbsp;&nbsp; 4.4&nbsp;&nbsp;&nbsp; Blue &nbsp; I stored this values in one of excel file so i want to read these values from file and I have to compare these values with the value I&nbsp;take from DAQ board, for example i&nbsp;measure the value fro...

String increment
Hi, I want a code that would generate strings like A1,A2.......A23, the reason I want it is because I am controlling excel with matlab and to enter the range I need those values to be incremented.....plz help me as my whole project depends on this Adi gahlawat wrote: > Hi, > I want a code that would generate strings like A1,A2.......A23, the > reason I want it is because I am controlling excel with matlab and to > enter the range I need those values to be incremented.....plz help me > as my whole project depends on this doc sprintf and friends -- ...

String/isUpperCase?
I'm new to java and could use some help with a program that reads a string and counts the Upper Case letters. I tried isUpperCase but it was only doing a character not the whole string. Do I need some sort of next command or can I change the string to something else? any help would be appreciated. Jim > I'm new to java and could use some help with a program that reads a string > and counts the Upper Case letters. I tried isUpperCase but it was only doing > a character not the whole string. Do I need some sort of next command or can > I change the string to something els...

Tcl based Internet Toolkit is now available for download and evaluation #2
http://tk.redolive.com ...

Why some thing evaluate to #t when they should evaluate to false
Having some experience with Python, the following things evaluate to False in a boolean sense: False, None, [], 0 etc However in scheme the following evaluate to #t when passed as a boolean to another function: '(), 0, etc. leading to extra typing such as (if (not (null? x)) (do-when-list-has-stuff) (do-when-list-is-empty)) or (if (zero? x) (do-false-thing) (do-true-thing)) when it seems more natural to me to do: (if x (do-true-thing) (do-false-thing)) where x can be a null list and works "like python" , since that is more familiar to me. How/why? Is it un-idiomatic or un-s...

binutils "strings" like functionality?
Hey all: I am working on a little script that needs to pull the strings out of a binary file, and then manipulate them with python. The command line utility "strings" (part of binutils) has exactly the functionality I need, but I was thinking about trying to implement this in pure python. I did some reading on opening and reading binary files, etc., and was just wondering if people think this is possible, or worth my time (as a learning exercise), or if something like this already exists. -cjl I don't know anything about binutils strings, other than that a quick google reve...

Strings with Templates not working?
Hello, I'm using a template to simulate a LinkedList from Java.It works without problems, but when I want to use strings in main.cpp instead of char*, I get the following error message: $ ./Main terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct NULL not valid I tried to import <string> both in main.cpp and listT.h, but the errormsg was the same. The classes look like this: MAIN.CPP------------------------------------------ #include "ListT.h" #include <iostream> using namespace std; int main(void)...

Evaluate & catch problems
The word FFF isn't defined. : y s" : x 1 2 fff ;" evaluate ; : z ['] y catch ; z This stays in compilation state with -13 (undefined) on the stack. If terminated with ; then X is defined as : x 1 2 -13 ; I suspect a really subtle bug. What should be the outcome? Op Wed, 5 Jan 2011 13:49:59 -0800 (PST) schreef Alex McDonald: > The word FFF isn't defined. > >: y s" : x 1 2 fff ;" evaluate ; >: z ['] y catch ; > z > > This stays in compilation state with -13 (undefined) on the stack. If How do you know -13 is on the stack, as Forth...

Extract Number From Text String
Hello, Does xHarbour have a function to extract Number from a text string? Regards Otto Dear Otto Haldi: On Monday, September 17, 2012 11:48:58 AM UTC-7, Otto Haldi wrote: .... > Does xHarbour have a function to extract > Number from a text string? http://www.ousob.com/ng/53guide/nga7e04.php val() David A. Smith On Mon, 17 Sep 2012 12:05:13 -0700 (PDT), dlzc <dlzc1@cox.net> wrote: Hello David, Thanks for your help. Yes, I know the function val() I mean for that kind of string : "abc-2344-2323". Regards Otto >Dear Otto Haldi: >...

String manipulation #2
I'm writing a program that requires some string manipulations. Let's say I have a string s='x' Now, the ascii code for 'x' is 0x78. I want to be able to perform operations on this number, and print the character corresponding to the results of the operation. For example, the pseudo-code looks like: -read in string s from external file (for simplicity, we'll keep s='x') -determine the code for s (should be 0x78 or 120 in base 10), store it in variable c -add a literal to c (let's say c=c+1) -find out which character corresponds to the new co...

How to "evaluate position" & "evaluate tangent" as Max 5 does with NURBS, math need?!
"evaluate position" "evaluate tangent" EvalPos EvalTan as Max 5 does with a set of controll points forming a NURBS curve. ...

strftime with format string %x
Hi All, I want to use %x as directive to get the locale specific date representation. But, at the same time, i want to know the format of the output for interpreting it through program. Is there a way to get the format of the %x directive (it differs across locales)? For ex, default (locale is C), %x gives like %m/%d/%y. Thank you Param param wrote: > Hi All, > > I want to use %x as directive to get the locale specific date > representation. But, at the same time, i want to know the format of > the output for interpreting it through program. Is there a way to get > th...

dynamic evaluation from a database
Hi, I am writing some functional testing applications in watir and I am most likely going to have this application store the data in a MySQL or Oracle database. Since the tests are going to be in a constant state of change I figure it might be best to store the scripts in the DB as well, since this would allow me to re-deploy any code changes by updating the clobs in the DB. The are several ways to go about this, but was curious as to writing stubs that just fetch the source directly from the DB using DBI to evaluate it. In a general sense is this asking for too much trouble in debugging or...

Re: Count a string variable
Monisan, You already have an answer. For your concete variables, count = count (lowcase (catx (",", of cre:)), "yes") ; or, more robustly, count = count (lowcase (catx (",", of cre1-cre10)), "yes") ; Kind regards ------------ Paul Dorfman Jax, FL ------------ On Wed, 30 Sep 2009 07:39:50 -0700, Monisan <mokaesa04@HOTMAIL.COM> wrote: >On Sep 29, 5:16 pm, Monisan <mokaes...@hotmail.com> wrote: >> I need to count the number of Yes in a group of variables. The >> variables are strings, so Yes is Yes not 1. Can I do this with...

evaluating quote
(As context for my question: I am working on evaluating procedure applications for the interpreter project in a class that is approximately following SICP.) (define exp (quote (+ 1 2))) (pair? exp) ; => #t (symbol? exp) ; => #f QUESTION 1: It is correct to say quote inhibits the normal evaluation of (+ 1 2), but it does not inhibit the conversion of (+ 1 2) from an external representation as a sequence of characters to an internal representation as a pair? QUESTION 2: Is the value of exp a chain of 3 pairs? "Marlene Miller" <marlenemiller@worldnet.att.net> writes: ...

How to mid string on a binary string?
I'm needing to take a binary string start at a certain position and return a pointer from that postion to the end of the binary stirng. something like this: char bstr[2048]; char *pos; pos=mid(bstr,35); / *return a pointer of the rest of the binary string starting at element 35 */ Any help showing me how I can do this? I don't know how I can do a char *mid(bstr,ele) Thanks, jt "jt" <jtsoft@hotmail.com> wrote in message news:lP3Ye.93210$4i6.18011@tornado.tampabay.rr.com... > I'm needing to take a binary string I'm not familiar with the concept of a...

string templates
I was justing wondering how safe python string templates are to use with unicode. I was just scanning pep 292 and it seems to say that they are -- or can by with inheritance... but I don't quite understand. -- David Bear -- let me buy your intellectual property, I want to own your thoughts -- David Bear wrote: > I was justing wondering how safe python string templates are to use with > unicode. I was just scanning pep 292 and it seems to say that they are -- > or can by with inheritance... but I don't quite understand. What do you mean with "safe"? I use string...

Menu/Kernel/Evaluation/Evaluate Notebook
Hi Group Is there a way to "know" whether the evaluation has been launched by the Front-End Menu command Kernel/Evaluation/Evaluate Notebook ? Is there an environment variable associated to this ? Thanks ! ...

How to underline a string in Java?
I thinked direct draw a line is not a good way.Also,I don't need the method of using HTML to implement this.Are there a good method to underline a string in java? "Bruce Sam" <persevreman@yahoo.com.cn> schreef in bericht news:1103080126.191175.246640@z14g2000cwz.googlegroups.com... >I thinked direct draw a line is not a good way.Also,I don't need the > method of using HTML to implement this.Are there a good method to > underline a string in java? If the API would support underlining, then directly drawing a line isn't a good way. But it isn't supp...