Click here to Skip to main content
15,885,891 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
i want to call function in matlab from c# inside for loop using com component
when i tried it i had this exception

Invalid callee. (Exception from HRESULT: 0x80020010 (DISP_E_BADCALLEE))

this is the code
C#
for (int j = 1; j < 8; j++)
            {               
                object result1;
                string name = path + j + ".JPG";
                matlab.Feval("WBC_SegProposed", 6, out result1, name, 0, j);
           ...........//code
}

the exception was for this instruction " matlab.Feval("WBC_SegProposed", 6, out result1, name, 0, j);"
what should i do ?
Posted
Updated 10-Feb-19 5:24am
Comments
Sergey Alexandrovich Kryukov 10-Jul-14 22:03pm    
There is no such concept, "call a control". You can call a method or a property getter/setter, but it does not matter how many times...
—SA
Esraa Saady 11-Jul-14 10:49am    
but it gave me exception, why ?
Sergey Alexandrovich Kryukov 11-Jul-14 13:00pm    
Hard to say, from this information. Do you have exact exception type, message, stack, as well as the same for InnerException?
—SA
George Jonsson 10-Jul-14 22:47pm    
Do you get this error the first time in the loop?
Esraa Saady 11-Jul-14 10:50am    
No at the second time in it

1 solution

Hi,
I'm sad to see all the unhelpful comments above when it is really Mathwork's poor implementation leading to this error. The solution is to simply include result = null in your for loop:

C#
for (int j = 1; j < 8; j++)
            {               
                object result1 = null; // <---resetting result back to null
                string name = path + j + ".JPG";
                matlab.Feval("WBC_SegProposed", 6, out result1, name, 0, j);
           ...........//code
}


your code works for the first iteration because "result" is set to null by default. Then it gets assigned a value in the first iteration and Feval throws an error due to passing a non-null variable in the second iteration.
 
Share this answer
 
Comments
[no name] 10-Feb-19 12:19pm    
The C# TryParse() does not require an out variable to be initialized. To assume that Matlab does and that anyone that does not know this is somehow deficient is not logical.

Op has not said their problem is fixed; maybe the "jpg" was not found; etc.

AND it's a "local" variable which gets created on every iteration.
Member 14145676 10-Feb-19 12:27pm    
Thanks for your comment. Its indeed a local variable. You are right that it should not make a difference in this case. I just had a similar problem where changing "results" from global to local fixed the same error. Hence why I'm surprised that Feval does not change result to null first thing internally if it causes a problem anyway.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900