Sunday, June 28, 2015

Xiaomi Mi Pad: am loving it

Apple got it right long back. Now Xiaomi got it right. I can't help but love using Xiaomi Mi Pad. One reason is its display. Unlike most other Android tabs, its 7.9 inch screen has 4:3 aspect ratio; just like an iPad mini. Common tabs (even the costlier ones), with 16:10 or 16:9 aspect ratio, are good for watching movies and playing games in landscape mode. But the screen dimension of Mi pad is better for reading ebooks, pdf, taking notes, editing and reading MS Office documents. The retina display provides crisp fonts and sharp images. With 360 gm weight, Mi pad is easy to carry and hold in one hand, for long. With light weight, sturdy built and excellent display, it feels like carrying and working with a notebook. Just hold it in portrait mode, in one hand and use it.

Another key feature of Mi Pad is its long battery life. I am using it almost for one month now. I carry it all day and use it for web surfing, reading, taking notes and for minor editing of documents. Throughout the day it remains connected with my cloud storage through Wifi. On an average I get 16 hr of battery till it reaches ~20%.

Nowadays am using my desktop only for extensive typing, editing and drawing. Rest, my Mi pad can do.

A. Lots have been written about the smooth interface of MIUI and  almost lag free multitasking in Mi pad. I concur those.

B. Xiaomi has not launched official accessories for Mi Pad in India. Am using MOFI flip cover for Mi Pad. It's nice.

Thursday, June 25, 2015

Batch script to automate simulations in MATLAB

Many of us use MATLAB to perform simulations. Often such simulations requires multiple runs of the same program. Suppose we are simulating an ODE -based model for a cell signaling pathway. We have created a main function (say Main.m) that execute the simulation while calling and using other functions. We want to simulate the same model with large number of different values of a particular parameter. One way of doing it, is to modify the code itself to run multiple simulations with different sets of parameter values. But, I would prefer not to tamper with the code. The other option is to change the parameter value and run the simulations one after another. That's bit cumbersome and unprofessional. Can't we automate the process?

Yes, we can. Don't hard code the parameters in the main code. Let it read the parameter values from a separate file. That can be a text or m-file. Make multiple folders having all the codes and parameter file. Change the values in the parameter files in these folders. Now use a script to run the simulation from each of these folder, one after another. Note that each of the sub-folder should have all the codes to run the simulations independently.

Even, you can use this strategy to, sequentially, run completely different simulations from different sub-folders. The only requirement is that you have to use same name (Main.m) for all the main functions. 

The batch script written bellow does exactly that. My PhD student Vimalathithan wrote the script. If you find it useful, do give him the credit.

This may not be the only way or best way to automate multiple runs. Please do write back, with your take on this issue.

:: -The script automates sequential repeated run of same Matlab code. 
:: -The user should enter the location of the parent folder which contains the Sub models
:: -The user should enter the location of matlab.exe file
:: -Every event is recorded in a LogFile.txt in the parent directory
:: -The scripts does the following job 
::  1.Creates a text file holding the name of the sub folders present in the parent directory
::  2.Executes each sub folder by invoking the main function (Main.m)
::  3. If the function is not found, the error message is recorded and the loop continues with the next sub folder.
::  4.If matlab throws any error, the error message is recorded and the loop continues with the next sub folder
::Enter the location of the Model 
set ModelDir=D:\Dropbox\modeling_tool\MATLAB_vimal\ODEmodeling\ODE1

::Enter the location of the Matlab.exe file
set MatlabDir=C:\matlab\bin\matlab.exe
::Do not make any changes in the code below
cd %ModelDir%
Echo Start time is %date%_%time%>LogFile.txt
::Lists the folders in the current directory and writes them in a text file
dir /A:D /O:N /B>List.txt
setlocal enableextensions enabledelayedexpansion
Echo off
::Counts the number of lines in the text file
for /F %%A in ('type "List.txt"^|find "" /V /C') do (
 set /A count=%%A
set /A num=1
::Executes the matlab code present in each sub folder
for /F "tokens=*" %%A in  (List.txt) do (
 Echo Executing !num!-%%A of %count%
 cd %ModelDir%\%%A
 IF EXIST ErrorFile.txt (
  del ErrorFile.txt
 IF EXIST %ModelDir%\%%A\Main.m (
  "%MatlabDir%" -nodisplay -nosplash -nodesktop -wait -r "run('Main.m');exit;"
  IF EXIST %ModelDir%\%%A\ErrorFile.txt (
   Echo %%A is not executed>>%ModelDir%\LogFile.txt
   Echo WARNING. Matlab has logged an error file in the location %ModelDir%\%%A>>%ModelDir%\LogFile.txt
  IF NOT EXIST %ModelDir%\%%A\ErrorFile.txt (
  Echo %%A is executed successfully>>%ModelDir%\LogFile.txt
 IF NOT EXIST %ModelDir%\%%A\Main.m (
  Echo %%A is not executed>>%ModelDir%\LogFile.txt
  Echo WARNING. Main function is not found in the location %ModelDir%\%%A>>%ModelDir%\LogFile.txt
 set /A num+=1
 cd %ModelDir%
Echo End time is %date%_%time%>>LogFile.txt
::Deletes the List.txt file
del List.txt
Echo on