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

  • Follow


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
0
Reply Michael 12/9/2010 12:30:21 AM

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.scheidiger@hs-furtwangen.de> wrote in message <idp7ut$dai$1@fred.mathworks.com>...
> 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
0
Reply Ed 12/13/2010 4:32:24 PM


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>...
> 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.scheidiger@hs-furtwangen.de> wrote in message <idp7ut$dai$1@fred.mathworks.com>...
> > 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
0
Reply alberto.gascon (5) 2/3/2012 5:39:22 PM

2 Replies
228 Views

(page loaded in 0.275 seconds)

Similiar Articles:













7/20/2012 2:19:03 AM


Reply: