We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › Processing in Matlab
Page Index Toggle Pages: 1
Processing in Matlab (Read 1893 times)
Processing in Matlab
Feb 28th, 2009, 4:22am
 
Now I have it working !!

See here for details:

http://home.earthlink.net/~cdunson/ProcessingMatlab.html

I'll post again when significant improvements are made...
Re: Processing in Matlab
Reply #1 - May 2nd, 2009, 10:44am
 
Got a lot further with this now.  Thought I would post and make sure that you like the direction I have taken, and maybe get some help before I show to the matlab community.

The design of PApplet was done in a very specific way that implements a strict policy.  All fair and good, but not very useful as a means for the interface to matlab.  Issues:

1. We want Proceesing to run within the matlab internal java engine, thus calling exit in java cause matlab to exit.  PApplet design doesn't work here

2. We need to be able to set member variables within an object such as window size, line thickness, etc.  Prior to drawing the plot.

3. We want to be able to make PApplet's draw calls on the plot after it is created so that the same type of scripting interface that is available to Processing progammers is available to matlab programmers via text in an M-file.

4. We need to use object-like syntax to allow matlab users the ability specify what within matlab we are referring to.

I ask you to please consider my goal of a test file, written in the matlab language that embodies these concepts:

% Bust some processing/matlab head...
baseSize = 100;
maxX = 1600 - baseSize;
maxY = 1000 - baseSize;

for ith = 1:500  % If we can do this 500x then we are likely not leaking.

     % Calculate next random window dimension
     nextWidth = baseSize + rand(1) * maxX;
     nextHeight = baseSize + rand(1) * maxY;

     % Create next window, ie. preset member variables:
     turb=MatlabPlot(nextWidth,nextHeight);

     % Give it time to finish drawing.  Important note here,
       % So far I have not figured out to make the above call
       % block until java finishes drawing ...
     pause(10);

     % Cast it out.  Arrrg.  Big leaks here, I can draw 10-15 plots
       % before matlab runs out of memory ...
     turb.frame.dispose();
     turb.destroy();
     clear turb;
     clear java;

     display([ 'Plot number: ' sprintf( '%d', ith), ' completed!']);

end

As you can see, I have the interface pretty much worked out in terms of structure.  The problems are that: a) the calls do not block; and b) the memory leaks are considerable.  Do any of you know how to clear all memory allocated by PApplet()?  All of this is possible because of this class:

import processing.core.*;
import processing.xml.*;

import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
import java.util.regex.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
import java.util.zip.*;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;

public class MatlabPlot extends PApplet {

     float angle = 0;
     float px = 0, py = 0;
     float amplitude = 30;
     float frequency = 0;
     float fillGap = 2.5f;
     int c;
     int wwidth;
     int hheight;
     
     PFont fontA;

     public MatlabPlot() {
           System.err.println("Default Constructor" + width + " x " + height);
           width = 640;
           height = 480;
           //PApplet.start((PApplet)this, new String[] { "--bgcolor=#DFDFDF", "MatlabPlot" });
     }

     public MatlabPlot( int w, int h ) {
           wwidth = w;
           hheight = h;
           create((PApplet)this, new String[] { "--bgcolor=#DFDFDF", "--resizeable", "MatlabPlot" });
     }

     
     public void setup() {
       size(wwidth, hheight);
     //   frame.setResizable(true);

        fontA = loadFont("/home/matlab/src/processing-1.0.3/examples/Basics/Typography/Words/dat
a/Ziggurat-HTF-Black-32.vlw");
        textFont(fontA, 32);
     
       noLoop();
     }

     public void draw() {
             //int useW = windowSize.width - insets.left - insets.right;
             //int useH = windowSize.height - insets.top - insets.bottom;
           //System.err.println("drawee " + useW + " x " + useH);

       for (int i =- 75; i < height+75; i++){
         // Reset angle to 0, so waves stack properly
         angle = 0;
         // Increasing frequency causes more gaps
         frequency+=.006f;
         for (float j=0; j<width+75; j++){
           py = i+sin(radians(angle))*amplitude;
           angle+=frequency;
           c =  color(abs(py-i)*255/amplitude, 255-abs(py-i)*255/amplitude, j*(255.0f/(width+50)));
           // Hack to fill gaps. Raise value of fillGap if you increase frequency
           for (int filler = 0; filler<fillGap; filler++){
             set(PApplet.parseInt(j-filler), PApplet.parseInt(py)-filler, c);
             set(PApplet.parseInt(j), PApplet.parseInt(py), c);
             set(PApplet.parseInt(j+filler), PApplet.parseInt(py)+filler, c);
           }
         }
       }
       fill(255);
       text("Hello Matlab !!", 100, 165);
       save("hello.tif");
     }

 public void destroy() {
     System.err.println("Destroy");
   
 }



 static public void main(String argss[]) {
           PApplet.main(new String[] { "--bgcolor=#DFDFDF", "--external", "MatlabPlot" });
 }    
}


Any thoughts?

THANKS
Page Index Toggle Pages: 1