Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use BufferedReader.read(char[] , int, int) with Matlab

127 views
Skip to first unread message

Michael

unread,
Dec 8, 2010, 7:30:21 PM12/8/10
to
Hi everyone!

I have troubles using the java.io.BufferedReader.read(char[] cbuf, int off, int len) method in MATLAB because of MATLAB's automatic conversion of Java's built-in variables (boolean, byte, short, long, int, double, float, and char) to MATLAB variables.

I can not pass a MATLAB char Array to the read method:
cbuf= =char(1:1024);
bufleng= BufferedReader.read(cbuf, 0, 1024);
Since MATLAB arrays are passed by value and any changes that a Java method makes to them are not visible to the MATLAB code. However, I need to access the changes to cbuf of the read() method.

In such a case it is suggested to creat (with MATLAB's javaArray command) a Java Array, rather than passing a MATLAB array. MATLAB's javaArray command does not support the built-in Java types and it's therefore impossible to creat a Java char array.

I bypassed this by creating a Java String Array, which I converter to a CharArray in a second step:
s= javaObject('java.lang.String', 'sdsdfsd');
charar= javaMethod('toCharArray',s)
Sadly MATLAB converts the returned Java char Array automaticlly into an MATLAB char Array.

To avoid this conversion I tried to interlace the methods in each other:
s= javaObject('java.lang.String', 'sdsdfsd');
buflen= javaMethod('read', BufferedReader, javaMethod('toCharArray',s),0,
1024);

It doesn't work either. Does somebody has a idea how to solve this problem? I would appreciate your guys help a lot!

Michael

Ed Yu

unread,
Dec 13, 2010, 11:32:24 AM12/13/10
to
Hi Michael,

I think the BufferedReader cannot be used in the way you described below... It takes a Reader object... So you should try:

>> s = java.lang.String('sdsdfsd');
>> cr = java.io.CharArrayReader(s.toCharArray,0,1024);
>> br = java.io.BufferedReader(cr);

Hope this works...

Ed.

"Michael " <michael.s...@hs-furtwangen.de> wrote in message <idp7ut$dai$1...@fred.mathworks.com>...

Alberto Gascón

unread,
Feb 3, 2012, 12:39:22 PM2/3/12
to
Hello, I am finding exactly the same problem Michael reported, but, I'm sorry, I don't quite understand the answer given by Ed...

I need to run the read() method, but no buffer seems to have a correct formart (except cbuf= =char(1:1024); for which I can't then access the changes)

Does anyone have tryed Ed's solution or figured out how to solve the problem? I would appreciate very much. Many thanks for your help.

Alberto.

"Ed Yu" wrote in message <ie5hqo$2n7$1...@fred.mathworks.com>...
0 new messages