Showing posts with label matlab. Show all posts
Showing posts with label matlab. Show all posts

Monday, December 7, 2015

Hide Matlab Start Bar / Busy Bar / Status Bar

Reclaim precious screen space by disappearing the "Start bar" (in older versions of Matlab) whose main purpose is saying Busy.

com.mathworks.mde.desk.MLDesktop.getInstance.getMainFrame.getStatusBar.getParent.setVisible(0);


Source: http://www.mathworks.com/matlabcentral/newsreader/view_thread/152888

Friday, September 14, 2012

Writing floating point multi-channel TIFFs in Matlab

In a previous post I've commented on how to read a multi-channel floating point TIFF in Matlab using the Tiff class (available since R2009b).
The Tiff class also permits to write all TIFF flavors. But since this TIFF format is so flexible, setting up everything for writing a file is not straightforward. Concretely I'm interested in writing a multi-channel floating point TIFF. Using the details found here, I've wrote this functions that wraps all the parameter setup for writing this type of TIFF files.
function writeTIFF(data, filename)
% writeTIFF(data, filename)
% writes data as a multi-channel TIFF with single prec. float pixels
   t = Tiff(filename, 'w');
   tagstruct.ImageLength = size(data, 1);
   tagstruct.ImageWidth = size(data, 2);
   tagstruct.Compression = Tiff.Compression.None;
   %tagstruct.Compression = Tiff.Compression.LZW;        % compressed
   tagstruct.SampleFormat = Tiff.SampleFormat.IEEEFP;
   tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
   tagstruct.BitsPerSample =  32;                        % float data
   tagstruct.SamplesPerPixel = size(data,3);
   tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
   t.setTag(tagstruct);
   t.write(single(data));
   t.close();
Sources: http://www.mathworks.com/matlabcentral/answers/7184#comment_15023http://www.mathworks.fr/help/techdoc/ref/tiffclass.hhttp://www.mathworks.fr/help/techdoc/ref/tiffclass.htmltml

Sunday, May 13, 2012

Read floating point multi-channel TIFFs in Matlab >=R2009b

For reading a TIFF image in Matlab usually the imread function suffices:

d1 = imread('myfile.tif'); 

This call will even load single channel f32 (32bit floating point) images. But for multi-channel f32 images will fail. This is not a surprise, since most of the applications don't even load the single channel f32 files.

Starting from version R2009b Matlab includes a new Tiff class that implements much more of the TIFF format, allowing to read and write many flavors of TIFF files. The call is slightly different from imread: 

t = Tiff('myfile.tif'); 
d2 = t.read();

Sources: http://compgroups.net/comp.soft-sys.matlab/reading-64-bit-tif-image/405858
http://www.mathworks.fr/help/techdoc/ref/tiffclass.hhttp://www.mathworks.fr/help/techdoc/ref/tiffclass.htmltml

Sunday, April 29, 2012

MATLAB: "Trace/breakpoint trap" after system call

The MATLAB's system call permits to run a program in a shell. But some programs have dynamic dependencies that conflict with MATLAB's environment. For instance:

[status, result] = system(['/usr/local/bin/convert']); 

will throw something like:


dyld: Library not loaded: /opt/local/lib/libtiff.3.dylib
  Referenced from: /opt/local/bin/convert
  Reason: Incompatible library version: convert requires version 13.0.0 or later, but libtiff.3.dylib provides version 11.0.0

/usr/local/bin/convert: Trace/breakpoint trap 

A solution consists in resetting the variable DYLD_LIBRARY_PATH (for OSX or LD_LIBRARY_PATH in Linux)  before running the program. This call should do the trick:

[status, result] = system(['export DYLD_LIBRARY_PATH=""; ' '/opt/local/bin/convert']); 

Source: http://www.alecjacobson.com/weblog/?p=1453

Wednesday, October 13, 2010

Ctrl-c signal catching from C program and from MATLAB's mex

In C the signal handling can be done by replacing the signal handle. The function signal(SIGINT,func) places the handler for  SIGINT with your definition of void func(int sig);

#include <signal.h>;
#include <stdio.h>;

/* * Ctrl-C signal catching infrastructure * */
/* This global variable is accessible from all the files using extern*/

short INTERRUPTED;  

/* This function that handles the signal just changes the state of 
   INTERRUPTED. This variable is checked periodically in the main loop*/
void sigproc_ctrlC(int sig)
{
   /* To access the global variable must be defined as extern */
   extern short INTERRUPTED;       
   INTERRUPTED =1;
   printf("Ctrl-C pressed\n");
}

int main() {
   /* * INTERRUPT SIGNAL HANDLING * */
   /* The global variable must be defined as extern */
   extern short INTERRUPTED;   

   /* Initial value for INTERRUPTED*/
   INTERRUPTED=0;

   /* Associate the signal handler to the SIGINT */
   signal(SIGINT, sigproc_ctrlC);  


   printf("Press Ctrl-C to interrupt\n");
   /* Infinite loop*/
   while ( !INTERRUPTED ) {  
      printf("INTERRUPTED=%d\n",INTERRUPTED);

   }
}



We cannot apply this to MEX files, because in the MATLAB's enviroment there is already a signal handler in place, and if we replace it for our own, then our handler stays there for the remaining of the MATLAB's execution.
We need to reset the original handler after finishing the execution of our code. 
The following program will do the trick, and also applies to the standard C programs. The sigaction(SIGINT, &act, &oact) function allow to replace the SIGING handler with (act) but also returns a pointer to the original one (oact).


#include <signal.h>;
#include <stdio.h>;
#include <mex.h>;

/* * Ctrl-C signal catching infrastructure * */
/* This global variable is accessible from all the files using extern*/
short INTERRUPTED;  

/* This function that handles the signal just changes the state of 
   INTERRUPTED. This variable is checked periodically in the main loop*/
void sigproc_ctrlC(int sig)
{
   /* To access the global variable must be defined as extern */
   extern short INTERRUPTED;       
   INTERRUPTED =1;
   printf("Ctrl-C pressed\n");
}

/* Global variables for storing the signal handlers */
struct sigaction act, oact;


void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]){
   /* * INTERRUPT SIGNAL HANDLING * */
   /* The global variable must be defined as extern */
   extern short INTERRUPTED;   

   /* Initial value for INTERRUPTED*/
   INTERRUPTED=0;

   /* Save and over-ride old SIGINT behaviour */
   act.sa_handler = sigproc_ctrlC;
   sigaction(SIGINT, &act, &oact);



   /* Infinite loop */
   while ( !INTERRUPTED ) {
      printf("INTERRUPTED=%d\n",INTERRUPTED);
   }

   /* Reset the original handler */
   printf("Resetting signal handler for SIGINT\n");
   sigaction(SIGINT, &oact, &act);
}



Sources:
http://www.mathworks.cn/matlabcentral/newsreader/view_thread/23450
http://newsgroups.derkeiler.com/Archive/Comp/comp.soft-sys.matlab/2006-06/msg01751.html

Tuesday, June 8, 2010

Matlab's mxMalloc/mxFree: inefficient for memory intensive algorithms

This was a real headache.
When programming Matlab mex programs, it is customary to use the Matlab's mxMalloc/mxFree routines to manage the memory. It seems that aligns the data (maybe for efficiency), and also have several garbage collecting features (on termination of the program).

It turns out that these routines are very slow and for memory intensive applications, i.e. dynamic lists.


Some reading about using malloc/free inside Matlab mex:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/162021

Friday, June 4, 2010

Debug Matlab MEX files with gdb in Linux / OSX

From the Mathworks helpdesk site here are some instructions for debugging mex files.

Make sure that the mex has been compiled with -g
>> mex -g function.c

Restart Matlab with:
shell> matlab -Dgdb
run -nojvm
>> dbmex on

Run the code:
>> function(param)

To access the debugger:
>> dbmex stop

To add a breakpoint at the gateway function (just before your code):
break mexFunction