Using Matlab from c# update variable issue -
in code shared below using matlab dll c# application.the values returned matlab object random variables expecting them change @ different calls. in matlab working normally( returning different values in each call) when click button again returning same values
here , powerreclog() should return different value whenever click button again
onum.lqia,onum.lqib,onum.lqic,onum.lqid not changing.
private void button1_click(object sender, eventargs e) // yeni olcum { var acctx = type.gettypefromprogid("matlab.application.single"); var matlab = (mlapp.mlapp)activator.createinstance(acctx); matlab.visible = 0; matlab.putworkspacedata("pr", "base", 0); onum = new onlineolcum(); try { onum.xpos = convert.todouble(textbox1.text); onum.ypos = convert.todouble(textbox2.text); label1.text += " " + onum.xpos + " " + onum.ypos + "\n"; double t1, t2, t3, t4; t1 = math.round(t1.mesafe(onum), 8); t2 = math.round(t2.mesafe(onum), 8); t3 = math.round(t3.mesafe(onum), 8); t4 = math.round(t4.mesafe(onum), 8); string s1, s2, s3, s4; s1 = t1.tostring(new cultureinfo("en-us")); s2 = t2.tostring(new cultureinfo("en-us")); s3 = t3.tostring(new cultureinfo("en-us")); s4 = t4.tostring(new cultureinfo("en-us")); matlab.execute("pr=powerreclog(" + s1 + ",1,160,1);"); onum.lqia = math.round((double)matlab.getvariable("pr", "base"), 8); matlab.execute("pr=powerreclog(" + s2 + ",1,160,1);"); onum.lqib = math.round((double)matlab.getvariable("pr", "base"), 8); matlab.execute("pr=powerreclog(" + s3 + ",1,160,1);"); onum.lqic = math.round((double)matlab.getvariable("pr", "base"), 8); matlab.execute("pr=powerreclog(" + s4 + ",1,160,1);"); onum.lqid = math.round((double)matlab.getvariable("pr", "base"), 8); } catch(exception ex) { messagebox.show(ex.message); } matlab.quit(); }
the behaviour describe above (the repeated values returned matlab) may make sense. code above seems create new instance of matlab whenever button1_click
function called. code builds , invokes (matlab?) function powerreclog
generates numbers expect random not - repeat @ each call button1_click
. presumably body of powerreclog
use matlab rand
, randn
, or randi
pseudorandom number generators.
if launch matlab desktop , type rand(50,1)
, 50x1 matrix of uniformly distributed pseudo random numbers. if exit matlab, re-launch , type command rand(50,1)
same 50 uniformly distributed pseudo random numbers!
according matlab documentation (why random numbers repeat after startup) random number generator (that rand functions draw random numbers from) resets same state @ startup.
to around (see same link), can control random number generation invoking rng('shuffle')
seeds random number generator based on current time rand functions produce different sequences of numbers after each time call rng.
so each time button1_click
invoked, can call (in matlab) rng('shuffle')
reseed random number generator. or, add command matlab startup.m
file have automatically happen each time matlab launched.
Comments
Post a Comment