Matlab switch-case issue - matlab

I got a strange issue when i try to run a multiple switch-case ( 6 concatened switch-case, from 2 to 11 choices, char choices).
the inputs for the m. file with the swith case come from a GUI with 6 buttongroup and radiobutton, but i guess this is not the problem.
In fact, the problem is that if i pass the values "manually", kinda:
evaluation= Holdem_Postflop_Decision_Tree('midv','rn','fd', 'oop', 'hu',25)
i got to the right solution, that is simply a string ( ideally, this will return to the GUI and will be replace in an EDIT-TEXT box):
evaluation =
bettiamo perchè oop e calla abb spesso per floatare.
Al turn decidiamo di c/c o c/r su blank in base alla size, se cade suited c/f.
se turn abbiamo c/c e river è blank siamo in modalità vita c/c bottom-mid case: cbetptkiamo perchè oop.
Se raisa foldiamo.
Instead, if i do
evaluation= Holdem_Postflop_Decision_Tree(handles.val, handles.act, handles.text, handles.pos, handles.num, handles.bb);
and
handles.val='midv';
handles.act='rn';
handles.text='fd';
handles.pos='oop';
handles.num='hu';
handles.bb=25;
i get this error
??? Undefined function or variable "evaluation".
Error in ==> Holdem_Postflop_Decision_Tree at 6998
Moreover, if i create a Main.m like so:
value= input (' Value', 's');
action=input(' Action: ','s');
texture= input (' Texture', 's');
pos=input(' Position: ','s');
num= input (' Opponumber', 's');
bb=input(' Big blinds: ');
evaluation= Holdem_Postflop_Decision_Tree(value, action, texture, pos, num,bb);
it does work!
I really dont get it: what's the difference? I checked that all handles.field are char, and if not, i've placed at the very beginning this:
value=char(value);
actionpre=char(actionpre);
texture=char(texture);
pos=char(pos);
opponumber=char(opponumber);
I thought it was a multiple switch case problem, because if i enter the Holdem_Postflop_Decision_Tree.m from the GUI and i make separate switch-case, Matlab runs it, while it stops at the third, but now i cant think it's a multiple s-c problem, since if i put the value manually it goes through it without problem.
Any help?
P.S: i'll add the code for the string above:
case 'midv'
switch actionpre
case 'rn'
switch texture
case 'fd'
switch pos
case 'ip'
switch opponumber
case 'hu'
evaluation=' No info yet.';
case 'multiway'
evaluation=' No info yet.';
end
case 'oop'
switch opponumber
case 'hu'
if bb>15
evaluation='Cbettiamo perchè oop e calla abb spesso per floatare. Al turn decidiamo di c/c o c/r su blank in base alla size, se cade suited c/f.se turn abbiamo c/c e river è blank siamo in modalità vita c/c bottom-mid case: cbetptkiamo perchè oop. Se raisa foldiamo. Al turn se non improviamo siamo sempre in c/f a meno di info che non foldi spesso alla 2barrel, in quel caso possiamo betptkare, se calla e non improviamo river o turn è suited siamo in c/f river';
else
evaluation='cbettiamo per brokare';
end
case 'multiway'
evaluation=' No info yet.';
end

Related

How can I convert this select statement to functional form?

I am having a couple of issues to put this in a functional format.
select from tableName where i=fby[(last;i);([]column_one;column_two)]
This is what I got:
?[tableName;fby;enlist(=;`i;(enlist;last;`i);(+:;(!;enlist`column_one`column_two;(enlist;`column_one;`column_two))));0b;()]
but I get a type error.
Any suggestions?
Consider using the following function, adjust from the buildQuery function given in the whitepaper on Parse Trees. This is a pretty useful tool for quickly developing in q, this version is an improvement on that given in the linked whitepaper, having been extended to handle updates by reference (i.e., update x:3 from `tab)
\c 30 200
tidy:{ssr/[;("\"~~";"~~\"");("";"")] $[","=first x;1_x;x]};
strBrk:{y,(";" sv x),z};
//replace k representation with equivalent q keyword
kreplace:{[x] $[`=qval:.q?x;x;"~~",string[qval],"~~"]};
funcK:{$[0=t:type x;.z.s each x;t<100h;x;kreplace x]};
//replace eg ,`FD`ABC`DEF with "enlist`FD`ABC`DEF"
ereplace:{"~~enlist",(.Q.s1 first x),"~~"};
ereptest:{((0=type x) & (1=count x) & (11=type first x)) | ((11=type x)&(1=count x))};
funcEn:{$[ereptest x;ereplace x;0=type x;.z.s each x;x]};
basic:{tidy .Q.s1 funcK funcEn x};
addbraks:{"(",x,")"};
//where clause needs to be a list of where clauses, so if only one whereclause need to enlist.
stringify:{$[(0=type x) & 1=count x;"enlist ";""],basic x};
//if a dictionary apply to both, keys and values
ab:{$[(0=count x) | -1=type x;.Q.s1 x;99=type x;(addbraks stringify key x),"!",stringify value x;stringify x]};
inner:{[x]
idxs:2 3 4 5 6 inter ainds:til count x;
x:#[x;idxs;'[ab;eval]];
if[6 in idxs;x[6]:ssr/[;("hopen";"hclose");("iasc";"idesc")] x[6]];
//for select statements within select statements
//This line has been adjusted
x[1]:$[-11=type x 1;x 1;$[11h=type x 1;[idxs,:1;"`",string first x 1];[idxs,:1;.z.s x 1]]];
x:#[x;ainds except idxs;string];
x[0],strBrk[1_x;"[";"]"]
};
buildSelect:{[x]
inner parse x
};
We can use this to create the functional query that will work
q)n:1000
q)tab:([]sym:n?`3;col1:n?100.0;col2:n?10.0)
q)buildSelect "select from tab where i=fby[(last;i);([]col1;col2)]"
"?[tab;enlist (=;`i;(fby;(enlist;last;`i);(flip;(lsq;enlist`col1`col2;(enlist;`col1;`col2)))));0b;()]"
So we have the following as the functional form
?[tab;enlist (=;`i;(fby;(enlist;last;`i);(flip;(lsq;enlist`col1`col2;(enlist;`col1;`col2)))));0b;()]
// Applying this
q)?[tab;enlist (=;`i;(fby;(enlist;last;`i);(flip;(lsq;enlist`col1`col2;(enlist;`col1;`col2)))));0b;()]
sym col1 col2
----------------------
bah 18.70281 3.927524
jjb 35.95293 5.170911
ihm 48.09078 5.159796
...
Glad you were able to fix your problem with converting your query to functional form.
Generally it is the case that when you use parse with a fby in your statement, q will convert this function into its k definition. Usually you should just be able to replace this k code with the q function itself (i.e. change (k){stuff} to fby) and this should run properly when turning the query into functional form.
Additionally, if you check out https://code.kx.com/v2/wp/parse-trees/ it goes into more detail about parse trees and functional form. Additionally, it contains a script called buildQuery which will return the functional form of the query of interest as a string which can be quite handy and save time when a functional form is complex.
I actually got it myself ->
?[tableName;((=;`i;(fby;(enlist;last;`i);(+:;(!;enlist`column_one`column_two;(enlist;`column_one;`column_two)))));(in;`venue;enlist`venueone`venuetwo));0b;()]
The issues was a () missing from the statement. Works fine now.
**if someone wants to add a more detailed explanation on how manual parse trees are built and how the generic (k){} function can be replaced with the actual function in q feel free to add your answer and I'll accept and upvote it

What does dot notation for classes, properties and methods mean?

I'm a relative newbie to programming, except having used basic capabilities of Matlab for several years (manipulating arrays, linear algebra, functions, scripts, etc.) and am very recently starting to explore the object oriented side of the program (which is also my first foray into any object oriented programming!).
My biggest sticking point right now is understanding what the heck this dot notation means. For example, say I write the simple class
classdef alchemy
properties
element
end
methods
% CONSTRUCTOR
function e = alchemy
e.element = ' ';
end
end
end
What does the e.element actually mean? Then typing into the command line
e.element = 'LEAD'
assigns the string 'LEAD' to one instance of the property element of the class alchemy.... Why does it do this?
Now say I add the method
function e = transmute(e)
oldElem = e.element;
if oldElem == 'LEAD'
e = 'GOLD';
elseif oldElem == 'GOLD'
e = 'LEAD';
else
disp('Oh no! This is a non-transmutable element.');
end
end
I can now put in the command line e = e.transmute and it will return
e =
GOLD
What is going on in this situation with the dot notation? Why does e.transumte apply the function transmute to e? I'm really confused by this whole notation, and none of Matlab's help-pages give me any deeper inkling. Any help would be appreciated!
The dot notation is used for a property/attribute of the object or for invoking a method.
It has both meanings.
name_object.abc
will give you the value of abc if abc is a property of name_object, or will invoke abc on name_object if abc it's a method.
The class (or object) in Matlab is a collection of properties and methods.
Maybe what is confusing is that your method transmute accepts the object (it should) but returns a string with the same variable name as the original object. It is more or less a "get value of" method. Not a transmute object.
In this way it returns the transmuted object.
function e = transmute(e)
switch e.element
case 'LEAD'
e.element = 'GOLD';
case 'GOLD'
e.element = 'LEAD';
otherwise
disp('Oh no! This is a non-transmutable element.');
end
end
You can also call the method like this:
e = transmute(e)
It is equal to
e = e.transmute

Matlab: regexp usage

I am going to start illustration using a code:
A = 'G1(General G1Airlines american G1Fungus )';
Using regexp (or any other function) in Matlab I want to distinctively locate: G1, G1A and G1F.
Currently if I try to do something as:
B = regexp( A, 'G1')
It is not able to distinguish G1 with the G1A and G1F i.e. I need to force the comparison to find me only case with G1 and ignore G1A and G1F.
However, when I am searching for G1A then it should still find me the location of G1A.
Can someone please help ?
Edit: Another case for A is:
A = 'R1George Service SmalR1Al C&I)';
And the expression this time I need to find is R1 and R1A instead.
Edit:
I have a giant array containing A's and another big vector containing G1, R1, etc I need to search for.
If you want to find 'G1' but not 'G1A' or 'G1F' you can use
>> B = regexp(A, 'G1[^AF]')
B =
1
This will find 'G1' and the ^ is used to specify that it should not match any characters contained with []. Then you could use
>> B = regexp(A, 'G1[AF]')
B =
12 32
to find both 'G1A' and 'G1F'.

MATLAB not enough input argument error

Maybe i'm missing something obvious, but i'm just starting matlab so...
I have created many user defined function and use them to solve another function.
each function works when tested separately, but when I try test the bigger function, i get this message:
Error using F1 (line 2)
Not enough input arguments.
Error in run (line 5)
[X,Y]=NewrRaph2(F1,F2,d1x,d1y,d2x,d2y,1,1,iter)
my function F1 is:
function [F1]=F1(x,y)
F1=4*x^2+y^3+28;
and my function Newraph2 is:
function [X,Y]=NewrRaph2(F1,F2,d1x,d1y,d2x,d2y,x,y,iter)
x=x;
y=y;
for n=1:iter
deltax=((-F1(x,y)*d2y(y))+(F2(x,y)*d1y(y)))/Jacob(dix(x),d1y(y),d2x(x),d2y(y));
deltay=((-F2(x,y)*d1x(x))+(F1(x,y)*d2x(x)))/Jacob(d1x(x),d1y(y),d2x(x),d2y(y));
x=deltax+x;
y=deltay+y;
end
X=x;
Y=y;
also, to test my function I use this script:
clear, clc
x=input('valeur de x ')
y=input('valeur de y ')
iter=input('valeur de iter: ');
[X,Y]=NewrRaph2(F1,F2,d1x,d1y,d2x,d2y,x,y,iter)
But I don't understand what I did wrong.

Perl syntax errors on declaring a large constant array

I want to declare a huge list of constant array like:
my #tlds = (ac ad ae aero af ag ai al am an ao aq ar arpa as asia at au aw ax az ba bb bd be bf bg bh bi biz bj bm bn bo br bs bt bv bw by bz ca cat cc cd cf cg ch ci ck cl cm cns
co com coop cr cu cv cw cx cy cz de dj dk dm do dz ec edu ee eg er es et eu fi fj fk fm fo fr ga gb gd ge gf gg gh gi gl gm gn gov gp gq gr gs gt gu gw gy hk hm hn hr ht hu id ie
il im in info int io iq ir is it je jm jo jobs jp ke kg kh ki km kn kp kr kw ky kz la lb lc li lk lr ls lt lu lv ly ma mc md me mg mh mil mk ml mm mn mo mobi mp mq mr ms mt mu museum mv mw mx my mz na name nc ne net nf ng ni nl no np nr nu nz om org pa pe pf pg ph pk pl pm pn pr pro ps pt pw py qa re ro rs ru rw sa sb sc sd se sg sh si sj sk sl sm sn so sr st su sv sx sy sz tc td tel tf tg th tj tk tl tm tn to tp tr travel tt tv tw tz ua ug uk us uy uz va vc ve vg vi vn vu wf ws xn xxx ye yt za zm zw);
But It throws errors:
1. Syntax error, near dm do dz
2. no such class mz, near "mw mx my mz"
ANy pointers on how to remove these errors?
If I use qw before that list it shows no errors, why? Whats wrong with the above declaration?
qw does quoting and separating for you.
my #foo = ( "bar", "baz" );
means the same as:
my #foo = qw( bar baz );
Having a stack of sequential unquoted values is just an error.
See the documentation for quote like operators.
The qw operator is a quoting operator, as are all of the other q* keywords (q qq qw qr qx) each takes a delimiting character (or pair in the case of braces) and treats everything within the delimiters as a string. Each of the operators do something different to the string, with qw splitting the string on whitespace to create a list.
When you write a series of barewords in Perl, you end up with a large nested chain of indirect object calls. Here is a short example without keywords (so that it is not a syntax error):
$ perl -MO=Deparse -e 'ac ad ae aero af ag ai al am an ao aq ar'
'ad'->ac('aero'->ae('ag'->af('al'->ai('an'->am('aq'->ao('ar'))))));
-e syntax OK
In your case, perl merrily went along parsing what looks like indirect object syntax until it encountered a keyword, which disrupted the chain and caused a syntax error.
If you had not used a keyword in your list, the code would have compiled fine, and then you would have gotten a runtime error about a missing method in a package. If you were running your code under the use strict; pragma (which you always should) then the final bareword would become a syntax error (since strict subs prevents promoting barewords to strings. That would have at least caught the error at compile time.
The important takeaway from this is that Perl has many quote-like operators that are effectively strings with special processing attached. Removing the quote-like operator will inevitably result in syntax errors, since arbitrarily formatted strings are not valid Perl. A list of the buitin quote-like operators can be found on the perlop manpage.
This is because when you omit qw/.../, your characters are treated as barewords, and when it comes to "do", which is a keyword, error is signaled.
EDIT: Even though my answer explains the reason of an error (well, I beleive so), #Quentin's suggestion is more constructive: do not use barewords (i.e. do use qw// in your example) to save your time catching errors like yours. For example, you have int in your list, which is also a keyword, lc (a function), etc.