top bar

Difference between revisions of "Matlab Configuration"

(Added information about adding DLLs to system path.)
(Update instructions)
Line 44: Line 44:
  
 
1. Install Micro-Manager in a path without any spaces (for instance: C:\Micro-Manager1.4.6).
 
1. Install Micro-Manager in a path without any spaces (for instance: C:\Micro-Manager1.4.6).
 +
 +
{{note|At least with recent versions of MATLAB, there is no need to install in a path without spaces (tested with MATLAB R2013a).}}
  
 
2. In Matlab command prompt
 
2. In Matlab command prompt
Line 50: Line 52:
  
 
for example:
 
for example:
  C:/Micro-Manager-1.4.6/ij.jar
+
  C:/Micro-Manager-1.4/ij.jar
  C:/Micro-Manager-1.4.6/plugins/Micro-Manager/MMAcqEngine.jar
+
  C:/Micro-Manager-1.4/plugins/Micro-Manager/MMAcqEngine.jar
  C:/Micro-Manager-1.4.6/plugins/Micro-Manager/MMCoreJ.jar
+
  C:/Micro-Manager-1.4/plugins/Micro-Manager/MMCoreJ.jar
  C:/Micro-Manager-1.4.6/plugins/Micro-Manager/MMJ_.jar
+
  C:/Micro-Manager-1.4/plugins/Micro-Manager/MMJ_.jar
  C:/Micro-Manager-1.4.6/plugins/Micro-Manager/clojure.jar
+
  C:/Micro-Manager-1.4/plugins/Micro-Manager/clojure.jar
  C:/Micro-Manager-1.4.6/plugins/Micro-Manager/bsh-2.0b4.jar
+
  C:/Micro-Manager-1.4/plugins/Micro-Manager/bsh-2.0b4.jar
  C:/Micro-Manager-1.4.6/plugins/Micro-Manager/swingx-0.9.5.jar
+
  C:/Micro-Manager-1.4/plugins/Micro-Manager/swingx-0.9.5.jar
  C:/Micro-Manager-1.4.6/plugins/Micro-Manager/swing-layout-1.0.4.jar
+
  C:/Micro-Manager-1.4/plugins/Micro-Manager/swing-layout-1.0.4.jar
  C:/Micro-Manager-1.4.6/plugins/Micro-Manager/commons-math-2.0.jar
+
  C:/Micro-Manager-1.4/plugins/Micro-Manager/commons-math-2.0.jar
 +
 
 +
{{note|At least in recent versions of MATLAB (tested with R2013a), the file classpath.txt is automatically generated and should not be edited manually, lest it be overwritten. If classpath.txt contains a comment to that effect, use instead this command:
 +
>> edit([prefdir '/javaclasspath.txt']);
 +
The first time you edit the file, it will have to be created. After restarting MATLAB, you can check that the paths have been included in the MATLAB classpath by typing
 +
>> javaclasspath
 +
at the command prompt.}}
  
  
Line 71: Line 79:
 
6. create a java object of class MMCcore
 
6. create a java object of class MMCcore
  
>> import mmcorej.*;<br>
+
>> import mmcorej.*;
>> mmc=CMMCore;<br>
+
>> mmc = CMMCore;
>> mmc.loadSystemConfiguration ('C:\Micro-Manager1.2\MMConfig_demo.cfg');<br>
+
>> mmc.loadSystemConfiguration ('C:\Micro-Manager1.4\MMConfig_demo.cfg');
 
+
  
The Micro-Manager API is described here: http://www.micro-manager.org/content/doc/mmcore_api/html/class_c_m_m_core.html
+
The Micro-Manager Core API is described here: http://valelab.ucsf.edu/~MM/doc/mmcorej/mmcorej/CMMCore.html
  
  
The following simple script will snap an image from an 8-bit camera and display it in matlab:
+
The following simple script will snap an image from an up-to-16-bit monochrome camera and display it in MATLAB:
  
 
  >> mmc.snapImage();
 
  >> mmc.snapImage();
  >> img=mmc.getImage();
+
  >> img = mmc.getImage(); % returned as a 1D array of signed integers in row-major order
  >> width=mmc.getImageWidth();
+
  >> width = mmc.getImageWidth();
  >> height=mmc.getImageHeight();
+
  >> height = mmc.getImageHeight();
  >> img2=typecast(img,'uint8');
+
  >> if mmc.getBytesPerPixel == 2
  >> img2=reshape(img2,[height,width]);
+
    pixelType = 'uint16';
  >> imshow(img2);
+
else
 +
    pixelType = 'uint8';
 +
end
 +
>> img = typecast(img, pixelType);     % pixels must be interpreted as unsigned integers
 +
  >> img = reshape(img, [width, height]); % image should be interpreted as a 2D array
 +
>> img = transpose(img);                % make column-major order for MATLAB
 +
  >> imshow(img);
  
 
{{Programming_Sidebar}}
 
{{Programming_Sidebar}}

Revision as of 09:32, 6 August 2013

Using Micro-Manager from your Matlab environment.

The Micro-Manager Core Java API is contained in the file MMCoreJ.jar. Any Java program (including Matlab) that wants to use the Micro-Manager API needs to put MMCoreJ.jar in its ClassPath. When CMMCore Java object is first created in the calling program it will automatically attempt to load native library MMCoreJ_wrap. This library must be visible to the Java run-time. Default locations and exact names of libraries are platform dependent.

On Windows, the native library file is MMCoreJ_wrap.dll and it must reside either in the system path or in the current working directory of the program in order to be detected by the Java run-time.

MMCore can be used in Matlab through its Java interface. After setting up the Java environment as described above, MMCoreJ.jar must be added to Matlab Java class path and the directory for the MMCore dynamic libraries (including MMCoreJ_wrap) must be added to the system path.


The following recipe has worked in the past for version <=1.3 If you are using MM of version 1.4.6 (as of 06/20/11 its the nightly build) than look for simpler instructions below:

Matlab has an extensive Java support which makes things easy, just do the following:

1. Install Micro-Manager in a path without any spaces (for instance: C:\Micro-Manager1.2).

2. Add the path with the drivers (all the .dll files) to the windows PATH enviromental variable (Start->Settings->Control Panel->System->Advanced->Environment Variables, make a new 'User variable for Administrator named 'PATH' and set it to the location of Micro-Manager (for instance: C:/Program Files/Micro-Manager-1.3).

3. In Matlab command prompt

>> edit classpath.txt 

Add the location of the jar file you just installed (its in the Micro-Manager folder), include the .jar at the end, for instance:

C:/Program Files/Micro-Manager-1.3/ij.jar
C:/Program Files/Micro-Manager-1.3/plugins/Micro-Manager/MMCoreJ.jar
C:/Program Files/Micro-Manager-1.3/plugins/Micro-Manager/MMJ_.jar
C:/Program Files/Micro-Manager-1.3/plugins/Micro-Manager/bsh-2.0b4.jar
C:/Program Files/Micro-Manager-1.3/plugins/Micro-Manager/swingx-0.9.5.jar

4. In Matlab command prompt

>> edit librarypath.txt 

Add the location of the dll files, for instance:

C:\Program Files\Micro-Manager-1.3

5. Restart Matlab

6. create a java object of class MMCcore

>> import mmcorej.*;
>> mmc=CMMCore;
>> mmc.loadSystemConfiguration ('C:\Micro-Manager1.2\MMConfig_demo.cfg');


The following recipe has worked for versions 1.4.6 - 1.4.11 (and newer hopefully):

1. Install Micro-Manager in a path without any spaces (for instance: C:\Micro-Manager1.4.6).

Note
At least with recent versions of MATLAB, there is no need to install in a path without spaces (tested with MATLAB R2013a).

2. In Matlab command prompt

>> edit classpath.txt 

Add the location of all the jar files MM installed under plugins\Micro-Manager:

for example:

C:/Micro-Manager-1.4/ij.jar
C:/Micro-Manager-1.4/plugins/Micro-Manager/MMAcqEngine.jar
C:/Micro-Manager-1.4/plugins/Micro-Manager/MMCoreJ.jar
C:/Micro-Manager-1.4/plugins/Micro-Manager/MMJ_.jar
C:/Micro-Manager-1.4/plugins/Micro-Manager/clojure.jar
C:/Micro-Manager-1.4/plugins/Micro-Manager/bsh-2.0b4.jar
C:/Micro-Manager-1.4/plugins/Micro-Manager/swingx-0.9.5.jar
C:/Micro-Manager-1.4/plugins/Micro-Manager/swing-layout-1.0.4.jar
C:/Micro-Manager-1.4/plugins/Micro-Manager/commons-math-2.0.jar
Note
At least in recent versions of MATLAB (tested with R2013a), the file classpath.txt is automatically generated and should not be edited manually, lest it be overwritten. If classpath.txt contains a comment to that effect, use instead this command:
>> edit([prefdir '/javaclasspath.txt']);

The first time you edit the file, it will have to be created. After restarting MATLAB, you can check that the paths have been included in the MATLAB classpath by typing

>> javaclasspath
at the command prompt.


3. In Matlab command prompt

>> edit librarypath.txt 

Add the location of the dll files, for instance: C:/Micro-Manager-1.4.6/

4. Add the location of the dll files to the system path. This is not required for all device drivers but is required to access the Andor driver on a Windows 7 system.

5. Restart Matlab

6. create a java object of class MMCcore

>> import mmcorej.*;
>> mmc = CMMCore;
>> mmc.loadSystemConfiguration ('C:\Micro-Manager1.4\MMConfig_demo.cfg');

The Micro-Manager Core API is described here: http://valelab.ucsf.edu/~MM/doc/mmcorej/mmcorej/CMMCore.html


The following simple script will snap an image from an up-to-16-bit monochrome camera and display it in MATLAB:

>> mmc.snapImage();
>> img = mmc.getImage();  % returned as a 1D array of signed integers in row-major order
>> width = mmc.getImageWidth();
>> height = mmc.getImageHeight();
>> if mmc.getBytesPerPixel == 2
    pixelType = 'uint16';
else
    pixelType = 'uint8';
end
>> img = typecast(img, pixelType);      % pixels must be interpreted as unsigned integers
>> img = reshape(img, [width, height]); % image should be interpreted as a 2D array
>> img = transpose(img);                % make column-major order for MATLAB
>> imshow(img);
© Micro-Manager : Vale Lab, UCSF 2006-2011 | All Rights Reserved | Contact