MATLAB API

The Matlab API can be used to execute commands, functions, scripts, send and pull data from a MATLAB environment. A walkthrough is available to send a trace to MATLAB.

Getting the Matlab Proxy

The IMatlabProxy object is what is used to communicate with MATLAB. Before an instance of this object can be used INTViewer must already be connected to MATLAB.

The code below creates an instance of the IMatlabProxy object.

    IMatlabProxy proxy = IMatlabConnector.Factory.getInstance().getProxy();

Sending a trace

Using a IMatlabProxy instance created like above we can now send data to MATLAB.

            ISeismicData seismicData = (ISeismicData) IData.factory.createNewInstance("mySeismicData.xgy");
            ISeismicReader seismicReader= seismicData.select(new SeismicRangeQuery());
            ITrace trace = seismicReader.getTrace(0);
            float[] samples = new float[trace.getSize()];
            trace.getSamples(samples); 
            proxy.setVariable("trace0", samples);

Getting a trace

Using the "trace0" variable crated above:

                 float[] matlabSamples = (float[]) proxy.getVariable("trace0");

Sending a volume

        ISeismicData seismicData = (ISeismicData) IData.factory.createNewInstance("mySeismicData.xgy");
        IKeyRange inlineKey = seismicData.getDataRange().get(0);
        IKeyRange xlineKey = seismicData.getDataRange().get(1);
        ISeismicReader reader = seismicData.select(new SeismicRangeQuery()); //select all data
        IFieldDesc inlineFieldDesc = reader.getTraceHeaderField(inlineKey.getName());
        IFieldDesc xlineFieldDesc = reader.getTraceHeaderField(xlineKey.getName());
        int numInlines = (int) (inlineKey.getMaximum() - inlineKey.getMinimum())+1;
        int numXlines = (int) (xlineKey .getMaximum() - xlineKey .getMinimum())+1;
        int numberOfSamples = reader.getDataStatistics().getSamplesPerTrace();
        int numberOfTraces = reader.getNumberOfTraces();
        double[][][] volumeData = new double[numInlines][numXlines][numberOfSamples];
        for (int i = 0; i < numberOfTraces; i++) {
            ITrace trace = reader.getTrace(i);
            float[] samples = new float[numberOfSamples];
            trace.getSamples(samples);
            int inline = trace.getHeader(inlineFieldDesc.getIdentifier()).intValue();
            int xline = trace.getHeader(xlineFieldDesc.getIdentifier()).intValue();
            for (int j = 0; j < samples.length; j++) {
                int inlineIndex = inline - (int)inlineKey.getMinimum();
                int xlineIndex = xline - (int)xlineKey.getMinimum();            
                volumeData[inlineIndex][xlineIndex][j] = samples[j];
            }
        }
        MatlabNumericArray matlabNumericArray = new MatlabNumericArray(volumeData, null);
        proxy.setNumericArray("volume", matlabNumericArray);

Note: This will not work for large seismic files, the data will have to be buffered in those cases.

Getting a volume

Using the "volume" variable created above:

        MatlabNumericArray numericArray = proxy.getNumericArray("volume");
        double[][][] matlabVolumeValues= numericArray.getRealArray3D();

Executing commands in MATLAB

Simple commands

Using the same "volume" variable again, we divide every value by 2:

                proxy.eval("volume = volume./ 2");

Commands that return values

               double[] returnedValues = (double[]) proxy.eval("length(volume)", 1);