Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Matlab's cryptic error messages

May 17, 2014
Sometimes MATLAB's error messages can be very cryptic and misleading. Following are examples of error messages which I encountered while trying to solve \(\mathbf{A}x=b\) in least square sense using MATLAB's lsqr function. %% Error 1 Error using iterapp (line 60) user supplied function ==> @(x,transp_flag)transform_X(x,transp_flag, ...) failed with the following error: Not enough input arguments. Error in lsqr (line 180) u = u - iterapp('mtimes',afun,atype,afcnstr,x,varargin{:},'notransp'); %% Error 2 Error using * MTIMES is not supported for one sparse input and one single input. Error in iterapp (line 29) y = afun * x; Error in lsqr (line 180) u = u - iterapp('mtimes',afun,atype,afcnstr,x,varargin{:},'notransp'); For an experienced MATLAB user, error 2 may be easier to locate because of the message: "Error using *". However, when I was actually working, I got error 1 which said "Not enough input arguments". It took me sometime to figure out that both the errors were related to mismatching datatypes in input parameters. Both the situations were actually identical, except that in first case I was using function handle Afun in place of matrix representation of \(\mathbf{A}\). Here are the actual statements which raised errors: x = lsqr(Afun, b, 1e-9, 100, [], [], x0); % Raises Error 1 x = lsqr(A, b, 1e-9, 100, [], [], x0); % Raises Error 2
And here is the code snippet for Error 2 above. The code can be modified easily to create function handle Afun (see Example 2 here). Notice the statement x0 = single(rand(size(b))*5); in the code snippet which changes the datatype and causes error. n = 50; on = ones(n,1); A = spdiags([-2*on 4*on -on],-1:1,n,n); b = sum(A,2); x0 = single(rand(size(b))*5); x = lsqr(A,b,1e-9,100,[],[],x0);
The take-away message: Always check datatypes when MATLAB's error messages don't make sense!
Read more ...

Font & old emacs

Jul 11, 2011
Tested on: CentOS release 5.6 (Final)

The newer version of emacs (23.x) has a nice option in menu to change the default font. But the older emacs (21.4.1) does not have these options. The older (& stable) versions of emacs are generally shipped with centos. Here are steps for changing the default fonts in emacs 21.4.1

Option 1 (for current sessions only)
  1. Open emacs.
  2. Left-click on mouse while holding 'Shift' key on keyboard. A Font-menu will pop-up.
  3. Play around with the menu & set required font.

Option 2 (for all future sessions)
We have to find the available system fonts and apply them to ~/.emacs file.
  1. Open terminal & execute following command: xfontsel
  2. Left-click on 'fmly' & others to select a value. You will be able to see how the font looks like in the same window. When you are satisfied with the font selected press 'Select' button. It will copy the font name with details to clipboard. Following are some of the examples of the fonts: -*-courier-medium-r-*-*-17-*-*-*-*-*-iso8859-* -*-lucidatypewriter-medium-*-*-*-14-*-*-*-*-90-iso8859-* -*-luxi mono-medium-r-*-*-17-*-*-*-*-*-ascii-*
  3. Open ~/.emacs file in any text editor and add following at the end of the file: (set-default-font "-*-*-*-*-*-*-*-*-*-*-*-*-*-*") Replace -*-*-*-*-*-*-*-*-*-*-*-*-*-* by your selected font name (like one of the examples shown above) Save ~/.emacs & re-launch emacs. You should be able to see the difference.
  4. There might be some delay before text is displayed in emacs. Add following to top of the ~/.emacs file for a workaround: (modify-frame-parameters nil '((wait-for-wm . nil)))
Read more ...

Matlab Font(s) - Windows

Jan 14, 2011
I do not find Matlab default font to be soothing to eyes, specially the fonts in figure windows. See more about fonts here. So, here are the instructions to change the fonts in matlab. Make sure that you have installed the font of your choice.
  • For editor, main window, GUI
    Navigate to File->Preference. Select "Fonts" in the left panel. Then follow the instructions over there. You can also check the 'Custom' tab for advanced settings.
  • For figure window
    Add following lines to your startup.m. See next section for details. set(0,'DefaultAxesFontName','DejaVu Sans'); set(0,'DefaultAxesFontSize',10); set(0,'DefaultAxesFontWeight','Bold'); set(0,'DefaultTextFontName','DejaVu Sans'); set(0,'DefaultTextFontSize',10); set(0,'DefaultTextFontWeight','Bold'); Change the font-name & size according your choice. You can find a list of installed fonts by typing following command at matlab prompt. listfonts
startup.m
Each time matlab is started, it searches all the matlab path for this file. The most common place to keep this file is: C:\Users\<user-name>\Documents\MATLAB\ This is good place to put/over-ride settings. Create this file, if it is not present.

I am having hard-time  to configure the same thing in Ubuntu. I will be posting the same, if I get some success.
Update: I was not able to get anti-aliased fonts in linux. But, I found a font called 'Andale Mono', which gives good result with aliased fonts. This link might help in some cases.
Read more ...

Programmer's fonts comparison

Jan 1, 2011
In my opinion every programmer should be very selective about fonts, as bad fonts can not only decrease efficiency but also bless you with eye problems and headaches. Here is my own comparison of fonts. You can also find other font comparisons on many websites or just google 'programmer fonts'. I have listed the fonts in order of my preference. All the fonts shown below are anti-aliased. If you are forced to use aliased fonts, you can try Andale Mono, I find it good even at bigger font size and bold font weight. This blog uses Droid Sans Mono from Google web fonts for all the code sections.

1. DejaVu Sans Mono, 11 pt



2. Inconsolata-dz, 12 pt



3. Consolas, 11 pt



4. Anonymous, 11 pt



5. Courier-New, 11 pt



6. Lucida Console, 11 pt



7. Verdana, 11 pt;



8. Segoe UI, 11 pt

Read more ...