1

In the code I shared below I am using matlab dll from c# application.The values returned by matlab object are random variables so I am expecting them to change at different calls. In Matlab it is working normally( returning different values in each call) but when I click the button again it is returning the same values

Here , powerRecLog() should return different value but whenever I click button again

onum.lqia,onum.lqib,onum.lqic,onum.lqid is 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();
        }

1 Answer 1

2

The behaviour that you describe above (the repeated values returned by MATLAB) may make sense. Your code above seems to create a new instance of MATLAB whenever the button1_Click function is called. The code then builds and invokes the (MATLAB?) function PowerRecLog that generates some numbers that you expect to be random but are not - they repeat at each call to button1_Click. Presumably the body of PowerRecLog use the MATLAB rand, randn, or randi pseudorandom number generators.

If I launch MATLAB from my desktop and type rand(50,1), I will get a 50x1 matrix of uniformly distributed pseudo random numbers. If I exit MATLAB, re-launch it and type the command rand(50,1) I get the same 50 uniformly distributed pseudo random numbers!

According to the MATLAB documentation (why do random numbers repeat after startup) the random number generator (that all rand functions draw random numbers from) resets itself to the same state at startup.

To get around this (see same link), you can control the random number generation by invoking rng('shuffle') which seeds the random number generator based on the current time so that the rand functions produce different sequences of numbers after each time you call rng.

So each time the button1_Click is invoked, you can call (in MATLAB) the rng('shuffle') to reseed the random number generator. Or, add this command to the MATLAB startup.m file to have this automatically happen each time MATLAB is launched.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.