Category Archives: Code

Normalize values in a matrix to be between 0 and 1 (Matlab Tips)

I’m run a function in Matlab and it requires a matrix of variables within 0 and 1. The way to normalize the values to be between 0 and 1, and transform back to the original matrix as follows:

b = [10; 0; 1; 1];

%% normalized matrix B to 0 and 1
normB = b - min(b(:));
normB = normB ./ max(normB(:))

%% transform normalized matrix B to the original matrix
B_sim = normB_sim .* max(b(:))

Make sure, when transforming normalized matrix to the original matrix, you provided the maximum value of the original matrix. It not, the code cannot be run.

Original source: (Link) 

How to export string with timeperiod to .dat file? (Re-Post)

Here a question (original link) that I ask at Matlab Community that the reader may found useful.


I have a large string that containing time period such as 23-May-2009 02:00:00 (as attached). Variable editor in Matlab is unable to display these variables, since the variables have more that 524288 elements. I want to export the variables to .dat file, so I can see the list of the variables. I try to use fopen and fprintf functions to export the variables, but unsuccessful. Any help? Thank you.


load hourlyperiod.mat
d = hourlyperiod ;
fid = fopen('iwant.dat','w') ;
for i = 1:length(d)
 fprintf(fid,'%s \n',d(i,:)) ;
fclose(fid) ;

Split excel worksheets to individual files

If we have about many sheets in excel, and save all the sheets as their own files will definitely was not about to do this by hand. it take a take time!. I found a good tips from the internet, to solve this problem,in which using the macro!.

Here the code:

Sub CreateNewWBS()
 Dim wbThis As Workbook
 Dim wbNew As Workbook
 Dim ws As Worksheet
 Dim strFilename As String
 Set wbThis = ThisWorkbook
 For Each ws In wbThis.Worksheets
 strFilename = wbThis.Path & "/" & ws.Name
 Set wbNew = ActiveWorkbook
 wbNew.SaveAs strFilename
 Next ws
End Sub



Remove the last part of every line in notepad++

If we have the lines:

and we want final lines become like this:

we can applied notepad++ to do it. The solution as follow:

1) Using notepad++ and go to the search. Please used the regular expression as search mode

2) search


index\ means search for word; .* means everything else; $ means the end of line.

3) then replace with


4) That should find everything after (and including) index.php, and replace it with just index.php.


(Repost) Batch Change/Rename File Extensions In Windows

A good tips from Please refer to the original article.


Batch Change/Rename File Extensions In Windows

Do you want to batch rename file extensions in one simple step? Changing each file extension separately can be a pain in some cases where you have to change extensions of more than 100 files.

One of method using command line. The method as follow:

  • Select the folder which has all the files who’s extension you would like to change. As an example I have made a folder named “test”, inside this folder I have placed four images in .jpg format. To change the extension of the files inside the folder, press Shift and right-click on the folder and select Open Command Window Here.

open command window

  • You will now see a Command Window with the current folderdirectory open. Now type the following command line,
ren *.(current extension name) *.(extension you want)
  • Since I want to change the extension from .jpg to .jpg, in my case the command line would be,
ren *.jpg *.jpg

change extension

  • Now all the files inside the folder will change to the extension you want. It’s that simple.

Grouping boxplot in matlab

Here an example from The example is about on how to create a boxplot for multiple group of variables. The example as follows:

x=rand(1000,6); %variable x
y=rand(1000,6); %variable y
D(:,1:2:12)=x; %rearrange x into d.
D(:,2:2:12)=y; %rearrange y into d

% first column of D, will fill with column of x, then second... 
% column of D will fill with 1st column of y. then the sequent... 
% will follow.


figure('color',[1,1,1]); %create a blank graph
boxplot(D,'factorgap',10,'color','rk') %make a box plot on those blank graph
axis([0 25 -1 5]) %not compulsory:rearrange back axis 

set(gca,'xtick',1.8:4.3:50) %put xtick to x-axis
set(gca,'ytick',0:10) %put tick to y-axis
set(gca,'xticklabel',{'Direct care','Housekeeping','Mealtimes','Medication','Miscellaneous','Personal care'}) %label to each x-axis

ylabel('Normalised Y');
 legend(findobj(gca,'Tag','Box'),'HBN04-01 multibed','YAB single ') %put legend to figure