0

I've written a Matlab script which it calls several functions within itself, and now I need to run it from VB.net. The Matlab script has no input from VB, but it has an image output and a number from the workspace. After watching this youtube video, and reading this page, I wrote the following code within VB.net: (In this code main.m is the name of Matlab script)

Public Class Form1
    Dim MatLab As Object
    Dim Result As String
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MatLab = CreateObject("Matlab.Application")
        Result = MatLab.Execute("cd D:\\main.m\")

    End Sub
End Class

Also, I tried to use these pages (link 1, link 2), but they returned errors.

1 Answer 1

2

cd stands for Change Directory. If you call cd D:\main.m\ you're trying to navigate to a directory called main.m (which does not exist).

You have to do this in two steps: Change the directory first, then execute the file.

'Change directory to "D:\".
Result = MatLab.Execute("cd D:\")

'Execute "main.m".
Result = MatLab.Execute("main")
Sign up to request clarification or add additional context in comments.

5 Comments

Dear @VisualVincent. It works (It plots the figure).Thanks a lot. How should I get other resulting outputs (I mean the numbers)?
@Yasina : Unfortunately I don't know much about MatLab, but you should be able to save it to an image file by executing the command saveas(gcf,'filename.png') (ref: how to save the content of the current figure as an image?). You can then load that image from VB.NET.
@Yasina : Sorry, didn't notice that you edited your comment until now. As I said before I know too little about MatLab to be able to help you directly, but from a bit of searching I managed to find the following, which may or may not help you: How do I extract data from MATLAB figures? and: How do I extract data points from a plot?
Thanks. I decided to save figure and variable in the database within Matlab, and it's enough. Thanks again.
@Yasina : Glad to hear. Good luck with your project!

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.