From: Kimberly on
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
From: Yair Altman on
"Kimberly " <k.moravec(a)cs.ucl.ac.uk> wrote in message <i0sg33$cgu$1(a)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
From: Kimberly on
"Yair Altman" <altmanyDEL(a)gmailDEL.comDEL> wrote in message <i0t3e6$68$1(a)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
From: Kimberly on
No, wait, there's memory mapping in the main bit on file I/O.
HOW did I miss it? Never mind.

Kimberly