Thread Subject: Reading from a file in c-mex s-function: odd bug

Subject: Reading from a file in c-mex s-function: odd bug

From: Kate J.

Date: 8 Jan, 2012 01:57:08

Message: 1 of 4

I hate to post a question about a simple syntax bug, but this has really got me stumped: I'm reading in 2 files to my c-mex s-function, both of which contain columns of double values. I've verified that both files have essentially identical formatting, although they contain different data. In my c-mex s-function, I can read from one file with no problem (verified by printing the values read in, to a separate output file). However, although I'm using exactly the same syntax to read in the values from the second file, I get huge nonsense numbers or 0.0000 values for every value being read in, instead of the correct double values.

Unless someone requests it, I won't post my code below, because I've verified over and over that the syntax being used for both files is the same, as is the code used to print to the output file. Each file is read in using its own for() loop and count variables, so it's not an issue of variables being overwritten, etc.

Any ideas about how the identical syntax can be used to read in 2 different files, and works fine for one of them, but causes nonsense values for the second file (which is actually the first file being read in my code)? Thanks for your input.

Subject: Reading from a file in c-mex s-function: odd bug

From: Rune Allnor

Date: 8 Jan, 2012 10:25:12

Message: 2 of 4

On 8 Jan, 02:57, "Kate J." <kmj.w...@gmail.com> wrote:
> I hate to post a question about a simple syntax bug, but this has really got me stumped: I'm reading in 2 files to my c-mex s-function, both of which contain columns of double values. I've verified that both files have essentially identical formatting, although they contain different data. In my c-mex s-function, I can read from one file with no problem (verified by printing the values read in, to a separate output file). However, although I'm using exactly the same syntax to read in the values from the second file, I get huge nonsense numbers or 0.0000 values for every value being read in, instead of the correct double values.
>
> Unless someone requests it, I won't post my code below, because I've verified over and over that the syntax being used for both files is the same, as is the code used to print to the output file. Each file is read in using its own for() loop and count variables, so it's not an issue of variables being overwritten, etc.
>
> Any ideas about how the identical syntax can be used to read in 2 different files, and works fine for one of them, but causes nonsense values for the second file (which is actually the first file being read in my code)? Thanks for your input.

This is the kind of thing where one needs to see
the exact code, both for writing and reading the
file.

If I were to guess at a cause, it would be that
you, either at the write or the read stage, tell
the compiler that you have numbers on double format,
when the numbers in fact are integers. Or something
like that.

Rune

Subject: Reading from a file in c-mex s-function: odd bug

From: Kate J.

Date: 8 Jan, 2012 18:54:09

Message: 3 of 4

Thanks for your input, Rune. What I discovered was that my 2 arrays that were having trouble were being declared as double type, when they *needed* to be declared as floats. As soon as I made this change, the problem disappeared.

I had always used floats and doubles more or less interchangably... apparently I need to do some documentation reading about the differences between these types! :P

Subject: Reading from a file in c-mex s-function: odd bug

From: Rune Allnor

Date: 8 Jan, 2012 19:05:39

Message: 4 of 4

On 8 Jan, 19:54, "Kate J." <kmj.w...@gmail.com> wrote:
> Thanks for your input, Rune. What I discovered was that my 2 arrays that were having trouble were being declared as double type, when they *needed* to be declared as floats. As soon as I made this change, the problem disappeared.
>
> I had always used floats and doubles more or less interchangably... apparently I need to do some documentation reading about the differences between these types! :P

When it comes to binary files, you need to
read the contents back from file *exactly*
like you wrote it to the file.

Rune

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com