5

Hello recently i am trying get the result using matlab function and matlab function can return double array like image but and then I got that double by object class in C# but I could not convert to double that object class some one could help me

I have solved this problem

        MLApp.MLApp matlab = new MLApp.MLApp();
        matlab.Execute(@"Path");
        object result = null;
        matlab.Feval("RemoveShadow", 1, out result, 12, 13);
        var res = (result as object[]).Select(x => (double[,])x).ToArray();
        object im = res.GetValue(0);
        double[,] d = (double[,])im;

I have solved this problem

5
  • Try doing a result.GetType() to see its exact type (in the debugger or doing Console.WriteLine(result.GetType()) if you are doing a console program) Commented Mar 10, 2015 at 8:40
  • but I could not convert to double that object class What didn't work? did you receive an error message? Commented Mar 10, 2015 at 8:42
  • I have recived the"An unhandled exception of type 'System.IndexOutOfRangeException' occurred in program.exe " Commented Mar 10, 2015 at 8:48
  • If you are getting an IndexOutOfRangeException That means that you are trying to access an index that does not exist in the array. I notice that you are calling res[1]. Are we sure that has a value? Arrays have a zero based index, which means the first element is at index 0 not index 1 Commented Mar 10, 2015 at 9:02
  • I am glad that you have fixed your problem. But never, update your question with answer. Either mark one of the asnwers as answer or add your own answer and mark this. Thanks. Good luck. Commented Mar 11, 2015 at 6:26

3 Answers 3

6

if all are double in object array than, alternative to above answer

double[] resultArray = Array.ConvertAll<object, double>
                                  (inputArray, x => (double)x);
Sign up to request clarification or add additional context in comments.

2 Comments

i need this array double[,]
Thank you I have got result
2

You can try this to convert object[] to double[]:

var res = (result as object[]).Select(x => (double)x).ToArray();

1 Comment

thank you for your answer but object class does not have Select method
-1

Correct way to handle this is

var res = (result as object[]).Select(x => Convert.ToDouble(x)).ToArray();

1 Comment

I tried his code and it didnt worked , I got InvalidTypeCaste exception because sometimes array might be having null. So above given solution have issue while handling null. where as if we use my solution you can control null type casting.

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.