I am actually working in MATLAB/Simulink, however, I need to create my own custom block, and I must use C code inside that block. My problem arises as such...

I read into Matlab a 480x640 matrix of "uint8" data, which I subsequently convert to format "double" (double precision). Simulink reads this matrix in and hands it to my custom C code.

When Simulink reads in the data, I declare it as format "double"...Matlab double. Which gets to the heart of my question. Is Matlab's double the same as C's "double?" I assume that they are, however, I am running into problems.

I have a very simple algorithm which computes the centroid of an object. To do this, I must access the data in the matrix, and in certain instances multiply by integers.

If I simply read the data from the matrix, I have no problems. But when I try to multiply by an integer "i" or "j" (declared as "int" at beginning of code) my answers are way off. I have tried declaring the variables "i" and "j" to "long", "unsigned long," and "double." I have also tried changing the input matrix back to "uint8" data. I get different answers with each combination of formats used.

Thus I am not sure where to go. When I declare the variables "i" and "j" as "double," the compiler tells me that I have

"operands of + have illegal types 'pointer to const double' and 'double'"

I have no idea what this means! Why can't I declare "i" and "j" to be double? FYI, "i" and "j" are variables which point to the current matrix location that I want to look at. They are cycled between 0-479 and 0-679, respectively.

I hope all the info is here. I appreciate any help anyone has to offer.

Thanks,

Matt

Hi mkimsal,

The word "double" is reserved to C and has an explicit meaning. If a variables is defined as:

double VarName;

then you can be sure that the variable conforms to C's understanding of what a double is.

If your variables are defined as a different type, you'll need to check the 'typedef' statements in your program and header files to see what data type is actually being used.

Can you give an example to you statement that, "my answers are way off". Remember that precedence in C covers data types AND operator order!

char a, b, c, d, e, f;

float A, B, C, D, E, F;

int j, k, l, m, n, o;

A = B * j; /* Will probably give you the answer that you want */

A = j * B; /* convers 'B' to an int, multiplies, and converts the answer to a float */

In second example above, if j = 1000 and b = 1.999, the result will be 1000.0!

If you're data values are being truncated, it's probably due to on-the-fly data conversion due.

Kent