Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
java memory mapped file in MATLAB?

Subject: java memory mapped file in MATLAB?

From: Kimberly

Date: 5 Jul, 2010 11:36:03

Message: 1 of 4

Hi all,

I'd like to load a file as memory mapped file in MATLAB... can't seem to do it as I get an error:

??? No appropriate method, property, or field MapMode for class java.nio.channels.FileChannel.

Sure enough it's not listed in methods FileChannel -full (though something similar is ???)

Maybe this is a static field or enum issue ... but I'm stumped as where to start. The analagous code in Java works fine.

Many thanks in advance for any help ... I can provide the bin file to any interested parties. The code I am using is listed below.

Many thanks,
Kimberly

import java.io.*;
import java.nio.*;
import java.nio.channels.*;

try
    in = FileInputStream(java.lang.String('data2_d_3.bin'));
    channel = in.getChannel();
    length = channel.size();
    %I want to create a MappedByteBuffer here ...
    buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, length);
    %... but instead I get the error:
    % ??? No appropriate method, property, or field MapMode for class
    % java.nio.channels.FileChannel.
    
    buffer.order(ByteOrder.LITTLE_ENDIAN); %because I wrote the file with MATLAB
    
    for p = 0:length/4
        ind = p * 4;
        disp(['line:' num2str(p) ' byte: ' num2str(buffer.getInt(ind))]);
    end
catch
   error('didn''t catch!');
end

Subject: java memory mapped file in MATLAB?

From: Yair Altman

Date: 5 Jul, 2010 17:06:14

Message: 2 of 4

"Kimberly " <k.moravec@cs.ucl.ac.uk> wrote in message <i0sg33$cgu$1@fred.mathworks.com>...
> Hi all,
>
> I'd like to load a file as memory mapped file in MATLAB... can't seem to do it as I get an error:
>
> ??? No appropriate method, property, or field MapMode for class java.nio.channels.FileChannel.
>
> Sure enough it's not listed in methods FileChannel -full (though something similar is ???)
>
> Maybe this is a static field or enum issue ... but I'm stumped as where to start. The analagous code in Java works fine.
>
> Many thanks in advance for any help ... I can provide the bin file to any interested parties. The code I am using is listed below.
>
> Many thanks,
> Kimberly
>
> import java.io.*;
> import java.nio.*;
> import java.nio.channels.*;
>
> try
> in = FileInputStream(java.lang.String('data2_d_3.bin'));
> channel = in.getChannel();
> length = channel.size();
> %I want to create a MappedByteBuffer here ...
> buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, length);
> %... but instead I get the error:
> % ??? No appropriate method, property, or field MapMode for class
> % java.nio.channels.FileChannel.
>
> buffer.order(ByteOrder.LITTLE_ENDIAN); %because I wrote the file with MATLAB
>
> for p = 0:length/4
> ind = p * 4;
> disp(['line:' num2str(p) ' byte: ' num2str(buffer.getInt(ind))]);
> end
> catch
> error('didn''t catch!');
> end

FileChannel.MapMode is an example of an inner class that is inaccessible from Matlab. If it had a constructor you could use:
  obj = javaObject('java.nio.channels.FileChannel$MapMode');
  mode = obj.READ_ONLY;

...but unfortunately it has no constructor. So the only way to access its static fields is to use Reflection. ugly...

BTW, I reported this problem with inner classes maybe two years ago. It is still not fixed as of R2010b.

But why do all of this if you can simply use the built-in Matlab function memmapfile?

Yair Altman
http://UndocumentedMatlab.com

Subject: java memory mapped file in MATLAB?

From: Kimberly

Date: 5 Jul, 2010 22:14:04

Message: 3 of 4

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <i0t3e6$68$1@fred.mathworks.com>...

[...]

Wow, thanks for this fantastically concise and specific answer!
 
> But why do all of this if you can simply use the built-in Matlab function memmapfile?
>
Only ignorance. It looks like memmapfile will do what I want perfectly.

THERE it is in the documentation ... way down in the release notes ... wonder what else is lurking down there? : )

Thanks again,
Kimberly

Subject: java memory mapped file in MATLAB?

From: Kimberly

Date: 5 Jul, 2010 22:18:04

Message: 4 of 4

No, wait, there's memory mapping in the main bit on file I/O.
HOW did I miss it? Never mind.

Kimberly

Tags for this Thread

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.

Contact us