views:

441

answers:

3

I have an array created in MATLAB that contains a number of cell type objects which contain arrays of doubles. It's basically a <1xn cell> array and each cell is an array of doubles.

What I want to do is to somehow export these so that I can then insert the data into Java as a ragged array of arrays of type int. Any thought on how to best do this?

A: 

You can call Java from MATLAB. Do a google search for Java and MATLAB. This looks like a good site for you: http://math.carleton.ca/old/help/matlab/MathWorks%5FR13Doc/techdoc/matlab%5Fexternal/ch%5Fjava.html

Chris Lacasse
A: 

Well I want to work from Java and not MATLAB so what I did was I adapted code from cell2cvs by Sylvain Fiedler and got this to do the job it gets the cell array and generates a txt file.

function cell2txt(datName,cellArray)
% Writes cell array content into a *.txt file.
%
% CELL2CSV(datName,cellArray,seperator,excelVersion)
%
% datName      = Name of the file to save. [ i.e. 'text.csv' ]
% cellarray    = Name of the Cell Array where the data is in
%
%         by Sylvain Fiedler, KA, 2004
% updated by Sylvain Fiedler, Metz, 06
% fixed the logical-bug, Kaiserslautern, 06/2008, S.Fiedler

seperator1 = ',';
seperator2 = '\n';

datei = fopen(datName,'w');

for z=1:size(cellArray,1)
    for s=1:size(cellArray,2)

        var = eval(['cellArray{z,s}']);

        if size(var,1) == 0
            var = '';
        end

        if isnumeric(var) == 1
            var = num2str(var);
            %fprintf(datei,seperator1);
        end

        fprintf(datei,var);

        if s ~= size(cellArray,2)
            fprintf(datei,seperator2);

        end
    end
end
fclose(datei);

Followup question here

Bar
+2  A: 

It's difficult to construct a Java array of primitives in Matlab, because Matlab wants to autobox it back into a Matlab array.

What you can do is create a Java class to help you, using the method signatures to guide Matlab's autoboxing. A wrapper layer like this may be faster and more convenient than a round trip through a text export.

package test;

/**
 * Class to help build Java arrays from Matlab.
 */
public class JavaArrayBuilder {
    /**
     * Assign an array into a larger ragged array
     * @param array ragged array you're building
     * @param i index into array
     * @param subarray this gets autoboxed to int[] from Matlab
     */
    public static void assignIntArray(Object[] array, int i, int[] subarray) {
        array[i] = subarray;
    }
}

Then you can call it from Matlab like this.

function ja = build_int_array

mynums = { 1:2, 1:5, 1:7 };
% Create a Java array of arrays
dummy = java.lang.Object();
ja = java.lang.reflect.Array.newInstance(dummy.getClass(), numel(mynums));
for i = 1:numel(mynums)
    test.JavaArrayBuilder.assignIntArray(ja, i-1, mynums{i});
end
% Now you have a Java ragged array, albeit as Object[] instead of int[][]

Afterwards, you'll need to convert the Object[] array to int[][] within Java, because Matlab will unbox a Java int[][] back to a Matlab array. Keeping it as Object[] within M-code protects it.

You could also build a List or other Collection using similar wrappers. That might mesh better with your other Java code, and Collections don't unbox in Matlab.

Andrew Janke
I'll give this a try during the weekend if I have time and get back to you
Bar