I am writing some 32 bit integer data in matlab using statements such
as:
f=fopen('temp','w');
a=int32(23);
fprintf(f,'%d',a);
fclose(f);
and trying to read in in JAVA using:
public static void main(String[] args) {
// TODO code application logic here
File file = new File("C:\\JAVA\\temp");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
int a;
a = dis.readInt();
System.out.print(a);
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}//end catch2
}//end main
======================
Now logically, reading a 32 bit integer using this command should work
fine, but I am getting out of range values, like, if i am just reading
in integer valued 32, it would rather print 8924558 or something like
that, garbage values. I have tried using read other reading methods as
well such as readByte, readShort, read etc but only thing that works
is readln, which I don't want. It brings in whole line of data, which
i don't want. Rather, I need individual integers to be read, perhaps
delimited by tabs or new lines
Joshua Cranmer - 19 Oct 2007 21:57 GMT
> Now logically, reading a 32 bit integer using this command should work
> fine, but I am getting out of range values, like, if i am just reading
[quoted text clipped - 4 lines]
> i don't want. Rather, I need individual integers to be read, perhaps
> delimited by tabs or new lines
A DataInputStream reads data as if it were binary, so the value `32'
would be written as `0011001100110010' in the file and then that
translated as an integer.
The easiest way to do what you want to do is to use Scanner:
<http://java.sun.com/javase/6/docs/api/java/util/Scanner.html>.

Signature
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
Patricia Shanahan - 19 Oct 2007 22:45 GMT
> I am writing some 32 bit integer data in matlab using statements such
> as:
[quoted text clipped - 3 lines]
> fprintf(f,'%d',a);
> fclose(f);
...
Did you mean to use text or binary? To do it in binary, compatible with
using DataInputStream, the Matlab code should be something like:
f = fopen('temp','w','ieee-be');
fwrite({f,a,'int32');
Java DataInputStream is big-endian and uses IEEE 754 format for floats,
so 'ieee-be' is the corresponding binary format for Matlab. "a" can be
any integer expression - I actually use the result of size applied to a
matrix, a pair of integers.
I've extracted this from some Java code that generates scripts and
invokes Matlab, so there may be detail errors, but the concept
definitely works.
I have a small package for using Matlab from Java. I can send you a copy
if that happens to be where you are heading.
Patricia
sbq - 26 Oct 2007 14:11 GMT
> > I am writing some 32 bit integer data in matlab using statements such
> > as:
[quoted text clipped - 25 lines]
>
> Patricia
thanks Patricia. The package may certainly be useful. Most of my work
is in MATLAB/VC but had to switch over to JAVA for Interfacing few
modules.
Patricia Shanahan - 26 Oct 2007 14:27 GMT
...
>> I have a small package for using Matlab from Java. I can send you a copy
>> if that happens to be where you are heading.
[quoted text clipped - 4 lines]
> is in MATLAB/VC but had to switch over to JAVA for Interfacing few
> modules.
My situation is the other way round. I have a Java program, in which I
needed to do a truncated singular value decomposition on a moderately
large sparse matrix, and Matlab svds was exactly what I needed.
Assuming your posting e-mail address is valid, expect an e-mail from me
sometime in the next few days with an attached zip file.
Patricia
Andrew Thompson - 26 Oct 2007 15:29 GMT
...
>...The package may certainly be useful. Most of my work
>is in MATLAB/VC but had to switch over to JAVA for Interfacing few
>modules.
Since it has not been explicitly mentioned, I will point out
that Java is a noun - a 'proper name' rather than an acronym
or abbreviation. So 'JAVA' is wrong, whereas 'Java' is correct.
Please (if you intend saying it often) get it correct.

Signature
Andrew Thompson
http://www.athompson.info/andrew/
Roedy Green - 26 Oct 2007 23:14 GMT
> dis = new DataInputStream(bis);
this is for writing binary values. Binary are not human readable.
See http://mindprod.com/jgloss/binary.html
if you want something human readable, you need to use a
BufferedWriter.
Get the file IO Amanuensis to generate you some code.
see http://mindprod.com/applet/fileio.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com