Cannot create ActiveX component/ File not found
显示 更早的评论
Hello again , am using vb.net and i have a matlab (.m) function and i want to run it inside vb.net so i used:
Dim Matlab As Object
Dim Result As String
Then i created a button so that when i press it it plots the graph of the matlab function using:
Matlab = CreateObject("Matlab.Application")
Result = Matlab.Execute("cd C:\Users\Bo$$\Documents\MATLAB\DF.m")
But i get this error:
(Cannot create ActiveX component)
on the command
Matlab = CreateObject("Matlab.Application")
So i tried:
Shell(Matlab.execute("DF"))
instead of the (Result) command. The plot appeared but i got this error (File not found) and i couldn't proceed with my program can you help me out thanks.
采纳的回答
Can you please use proper formatting (i.e. the {}Code button that's above the edit box when you write/edit your question) rather than inventing your own formatting.
I don't see how that Shell command would even work since the first argument to Shell must be the path of the executable. Not a VB object.
If
Matlab = CreateObject("Matlab.Application");
does not work then most likely Matlab is not registered properly as a COM server. Follow the instructions there
Note that your
cd C:\Users\Bo$$\Documents\MATLAB\DF.m
in your matlab execute is meaningless (unless DF.m is a folder. If you meant to call the script/function DF.m then the proper syntax would be:
Result = Matlab.Execute("C:\Users\Bo$$\Documents\MATLAB\DF.m")
with no cd at all.
Note that as documented in CreateObject, you're using late binding which is not recommended (slow). You could use early binding instead by adding to matlab on the COM tab of your project property. Then your code would be simply
Dim matlab As Matlab.Application
matlab.Execute("C:\Users\Bo$$\Documents\MATLAB\DF.m")
7 个评论
Sorry about the {} i was afraid the code gets mixed up with the words. SO about matlab i was able to add by going to (Projects\Add Reference\COM) from there i was able to choose matlab Application. And when i used:
Result = Matlab.Execute("C:\Users\Bo$$\Documents\MATLAB\DF.m") without cd it brought me back to the first error (Cannot create ActiveX component). And as you said the (CreateObject) is very slow, and when i tried
Dim matlab As Matlab.Application matlab.Execute("C:\Users\Bo$$\Documents\MATLAB\DF.m") i got this error (Type 'Matlab.Application' is not defined.) knowing that i have already added matlab from the COM tab. thank you.
I took a punt on the name of the CoClass that get imported into your project. Clearly it's not Matlab.Application. If you use the object browser in your project you'll see the correct name of the class. Using excel class browser, I suspect it's probably MLApp.MLApp, so:
Dim matlab As New MLApp.MLApp
matlab.Execute(...
yes it is MLApp.MLApp but sorry it didn't work either, anyway thanks for your help.
It didn't work either
In what way? Please post the exact code you're using and the exact error message you're getting if any.
The code am using the last part is :
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim matlab As New MLApp.MLApp
matlab.Execute("C:\Users\Bo$$\Documents\MATLAB\DF.m")
And the error i got was: Retrieving the COM class factory for component with CLSID {BD5FA15B-396A-431A-9E0A-E249168AAB63} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)). you know the only thing that worked was when i used:
Dim Matlab As Object
Dim Result As String
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Matlab = CreateObject("Matlab.Application")
Shell(Matlab.execute("DF"))
but the program stopped because of the error (File not found).
The only thing that worked was ...
Well, no. By your own statement it didn't work since you get a file not found error. And it's no wonder it doesn't work you're trying to run the result returned by matlab.Execute as a shell command. If you were to run matlab through the shell then you'd have to pass the path to matlab and you don't need to create a COM object.
I'm not too sure of the reason for your CO_E_SERVER_EXEC_FAILURE. Are you sure that matlab is activated and has a valid license in the context in which your VB program execute?
I've just created a very basic VB.Net program in VS2015 with the following code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim matlab As New MLApp.MLApp
Dim result As String
matlab.Visible = True
result = matlab.Execute("cd C:\users\xxx\Documents\matlab\answers\")
result = matlab.Execute("answer")
End Sub
End Class
After adding a reference to Matlab of course. This executes without any issue. Matlab COM server starts (you can see its window) and executes the script as required.
I've also tried with late binding:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim matlab As Object
Dim result As String
matlab = CreateObject("Matlab.Application")
matlab.Visible = True
result = matlab.Execute("cd C:\users\xxx\Documents\matlab\answers\")
result = matlab.Execute("answer")
End Sub
End Class
Again, no issue.
It worked it worked. I don't know what happened but when i tried your code it worked like charm. thank you so much MR.Guillaume and sorry for all the trouble i caused you.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Use COM Objects in MATLAB 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
