Friday, May 30, 2008

MATLAB Warning: Initializing Handle Graphics failed in matlabrc.

Hello Folks!
I'm finally here again though my spare time is almost nonexistent, to post a solution about a "MATLABonLinux" issue.

Yesterday after the month-long reorganization of my disks and data on my Desktop PC with the reinstallation of ArchLinux as OS... and in the same time after the failure of my old Laptop with the migration to another old laptop of all data and the installation from scratch of ArchLinux also there...
I installed MATLAB R2007a on ArchLinux under a 2.6.25 kernel, with JRE 1.6.0_05-b13 and Xorg 1.4.0.90

After a clean installation and a clean setup with the install_matlab script you have to run after the graphic install... I opened the program to see this message as output of the initialization process as it was just before the prompt:

Warning: Initializing Handle Graphics failed in matlabrc.
This indicates a potentially serious problem in your MATLAB setup, which should be resolved as soon as possible. Error detected was:
MATLAB:badsubscript
Attempted to access monitors(1,:); index out of bounds because size(monitors)=[0,4].

In matlabrc at 108

I began to search the Internet about this problem, but I found only references to a locale or pathdef problem I hadn't.

I tried to understand who called who in the initialization and going to matlabrc.m line 108 I found the call to another configuration/initialization script: hgrc.m
(All these configuration files are locate in your $MATLAB/toolbox/local/ directory where $MATLAB is your MATLAB installation path).

In hgrc.m I found the call to "get" to assign the matrix monitors who creates the issued that brought to the error.

In the MathWorks techincal documentation site you can find all the "Handle Graphics Object Properties" at this page
If you easily click on the root category of the properties in the list on the left you will find the very "MonitorPosition" property whose call led to an initialization error.

Actually the "get" call in hgrc.m assign to monitors the result of the MonitorPosition parameters that the MATLAB system recognized.

Probably due to a Java vs X incompatibility (I can't install FemLab because of a Java - X version to version issue) the get function returns and empty matrix for the call about MonitorDefinition

The solution is quite brutal. I read what data should output the get(0,'MonitorPosition') call, and I assigned them directly to the monitor variable modifying the hgrc.m script.

As you can find in the previous linked documentation page:


MonitorPosition
[x y width height;x y width height] Width and height of primary and secondary monitors, in pixels. This property contains the width and height of each monitor connnected to your computer. The x and y values for the primary monitor are 0, 0 and the width and height of the monitor are specified in pixels. The secondary monitor position is specified as
x = primary monitor width + 1
y = primary monitor height + 1
Querying the value of the figure MonitorPosition on a multiheaded system returns the position for each monitor on a separate line.
v = get(0,'MonitorPosition')
v =
x y width height % Primary monitor
x y width height % Secondary monitor
Note that MATLAB sets the value of the ScreenSize property to the combined size of the monitors


The monitors array for a 1024x768 monitor as mine should be:
monitors = [0,0,1024,768;1025,769,1024,768];

and for a 1280x1024 monitor as my desktop:
monitors = [0,0,1280,1024;1281,1025,1280,1024];


So in the end I commented this line in hgrc.m:
monitors = get(0, 'MonitorPosition');

inserting a '%' character before the line... and I added the manual assign of the correct matrix to the monitors variable:
monitors = [0,0,1024,768;1025,769,1024,768];

After this workaround my MATLAB starts without any problem and displays every figure correctly.

That's all folks.. I hope this solution is useful for someone...

Keep On Hacking!
bYe,
Andy

17 commenti:

  1. The fix worked find!!

    I'm in the end of curse and today i'had this problem with matlab!! I was caput without X functionalities of matlab!

    Good article!

    ReplyDelete
  2. I can get this to work with my monitor. However, I use a laptop. Sometimes I like to use the laptop screen and sometimes I use an external monitor. Is there any way to set this up where it works right all the time without having to adjust the hgrc.m every time I fire up MATLAB?

    ReplyDelete
  3. Well, I think you can prepare 2 arrays monitors_laptop and monitors_external and insert in hgrc.m an input statement to interact with the process while starting matlab.

    I coded this, I've tested (stand alone) and it should work also in hgrc.m ;-)
    'Copy & Paste' and hack it for your needs

    bYe,
    Andy



    % monitors = get('MonitorPositions');
    monitors_laptop = your_laptop_array;
    monitors_external = your_external_array;


    % Menu Cycle
    a=0;
    while(a~=1 & a~=2)
    disp('Type your choice:');
    disp('1. Laptop');
    disp('2. External Monitor');
    a = input('choice: ');
    end

    % Exit Switch
    switch(a)
    case 1
    monitors = monitors_laptop;
    case 2
    monitors = monitors_external;
    otherwise
    monitors = monitors_laptop;
    error('There is some trouble in hgrc.m! Check it please!');
    end

    ReplyDelete
  4. Thank You for such nice solution. Is Mathworks informed about this bug?

    ReplyDelete
  5. Hi, thank you for the comment!
    I don't know if MathWorks is aware of this bug...
    I think this is a conflict between those specific Java and X versions... So it shouldn't be a MathWorks issue.

    Have a nice day,
    bYe,
    Andy

    ReplyDelete
  6. Anonymous1:32 AM

    Thank you very much! I have the same setup and had the same problem, which is now fixed.

    I also encounter a Java exception on startup of Matlab, which I guess is related to this:

    Locking assertion failure. Backtrace:
    #0 /usr/lib/libxcb-xlib.so.0 [0xb5b9b767]
    #1 /usr/lib/libxcb-xlib.so.0(xcb_xlib_unlock+0x31) [0xb5b9b8b1]
    #2 /usr/lib/libX11.so.6(_XReply+0x244) [0xb5f0a9e4]
    #3 /opt/matlab/sys/java/jre/glnx86/jre1.5.0/lib/i386/xawt/libmawt.so [0xa1cd526a]
    #4 /opt/matlab/sys/java/jre/glnx86/jre1.5.0/lib/i386/xawt/libmawt.so [0xa1cbb352]
    #5 /opt/matlab/sys/java/jre/glnx86/jre1.5.0/lib/i386/xawt/libmawt.so [0xa1cbb599]
    #6 /opt/matlab/sys/java/jre/glnx86/jre1.5.0/lib/i386/xawt/libmawt.so(Java_sun_awt_X11GraphicsEnvironment_initDisplay+0x24) [0xa1cbb7a4]
    [...]

    But it works anyways now :)

    ReplyDelete
  7. Thanks a lot ! Your fix allow me to have decent plots again.
    For what it's worth, this seems to me not a JAVA problem but a X problem.

    When running matlab -nojvm which disables java, the same error message appears and plots are on a black background on my archlinux machine.

    cheers!

    ReplyDelete
  8. Thanks for finding the cause of the error. Here's a little mod to the script which should take care of different screen sizes:

    %monitors = get(0, 'MonitorPositions');
    [mon_stat, mon_sdim] = system( ...
    'xdpyinfo | grep "dimensions:" | tr -s " x" "," | cut -f3-4 -d","');
    mon_dim = eval(['[', mon_sdim, ']']);
    monitors = [0,0,mon_dim; mon_dim+1,mon_dim];

    It works for me under Slackware 12.2
    Have Fun!

    ReplyDelete
  9. To kill Java exceptions when starting Matlab:
    cd into matlab/sys/java/jre/glnx86
    mv jre old_jre
    ln -s /usr/lib/jvm/java-6-sun

    This is on Debian. This just tricks Matlab into using your system JRE instead of its own by making a symbolic link to your system's JRE (under debian, SUN Java JRE is usually found under /usr/lib/jvm/java-6-sun)

    ReplyDelete
  10. Thanks, worked perfectly!

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. Thanks for a very useful post on the MonitorPosition bug.

    fwiw, matlab can be forced to use the system jre with the env variable MATLAB_JAVA: On debian set MATLAB_JAVA=/usr/lib/jvm/java-6-sun/jre in your .bashrc, for example. Somewhat neater than the suggestion above.

    ReplyDelete
  13. I run a large-ish installation of matlab systems, so rolling out this patch for every system was not ideal.

    I contacted mathworks, and they verified this is a bug in the way matlab communicates with the xinerama libraries > 1.0.2.1.

    An additional workaround to this would be to downgrade Xinerama libraries to version 1.0.2.1. This can be accomplished by copying /usr/lib/libXinerama.so from an older system and placing it in /sys/os/$ARCH.

    This bug is supposedly fixed in matlab R2009b.

    ReplyDelete
  14. Anonymous9:46 AM

    http://www.mathworks.cn/matlabcentral/newsreader/view_thread/235875

    ReplyDelete
  15. Anonymous10:15 PM

    Yeah, you saved, OK, not directly my thesis, but at least my day, oh, better my night... anyway, cheers and thanks!

    Hecke

    ReplyDelete
  16. Anonymous10:36 AM

    Thanks from me too. I'm really glad I didn't have to track down this bug myself.

    - Bev

    ReplyDelete
  17. article that you created is very interesting, by reading this article I have a reference to write a new post on my blog

    ReplyDelete