tags:

views:

47

answers:

2

Hi,

i successfully integrated my .NET Dll into MATLAB. Everything is good. But, i pass a System.Double with value like 6000.46, and return it in a MATLAB function as [valueFromDotNet] the ans of MATLAB is ans = 6000, but i expected at least ans = 6000.4600 has anybody an idea where i have to look?

EDIT 1: I ensured that the format is short. (if get(0,'Format')) and i also set the format long; Nothing has changed. Anybody an idea why this is happening.

EDIT 2: found the problem. I receive the data that i feed into MATLAB from an external service. the numbers there are formated as string, but always as "6000.46"; i parsed with CultureInfo.InvariantCulture, but that seemed to be wrong. Changed it to new CultureInfo("en-US"), and it works now!

EDIT 3: i was too excited. it's still stange. but i think it's now just a not knowing how mathlab works

i have this callback

function tbmxHandleTickEvent(source,arg)
   t = arg.Tick;
   [t.BidPrice t.AskSize t.AskPrice t.LastSize t.LastPrice]
end

the callback gets triggered by an .NET event using this, i get the prices values without the fraction

when i change the callback like this

function tbmxHandleTickEvent(source,arg)
   t = arg.Tick;
   t.BidPrice
end

the result is this price including the fraction

A: 

The first thing to look at is the formatting of numbers in your Matlab console -- make sure that the number is 6000 not 6000.46 presented without the fractional part.

High Performance Mark
thanks. i tried something like valueFromDotNet==uint32(valueFromDotNet) which returns 0. how do i change the format? i'm new to matlab. thanks.
esskar
You would use the format command in Matlab, but you seem to have figured that out for yourself. It's difficult to provide any further advice without seeing (some of) your code.
High Performance Mark
+2  A: 

What might be happening is that one of those other values coming back is an integer type and that's causing the double to get converted when you concatenate them. Unlike most other programming languages, when combining floating point and integer types, Matlab will narrow the floating point (double) values to the integer types instead of widening the ints.

Here's an example in R2009b.

>> x = [6000.46 int32(1)], class(x)
x =
        6000           1
ans =
int32
>> 
>> 6000.46 + int32(1)
ans =
        6001

You can diagnose this, at least for concatenation, by enabling the warning for this lossy conversion. Try doing that and rerunning your code.

>> warning on MATLAB:intConvertNonIntVal
>> x = [6000.46 int32(1)]
Warning: Conversion rounded non-integer floating point value to nearest int32 value. 
x =
        6000           1
>> 

If this is what's going on, you should explicitly convert all the int values in arg.Tick to double. Maybe by extracting arg.Tick to a struct and converting while you step through the fields.

Andrew Janke
thanks. that's it! helped me alot!
esskar