Thursday, 11 December 2014

How to fit (curve-fit) a histogram using Gaussian / Normal distribution, in Matlab?

1. Use 'histfit'


histfit(DATA,NBINS)


%By default it will fit the histogram with the normal distribution.


2. There are lots of other distributions supported by the 'histfit'. The details are given in this Matlab link. Quick look of supported distributions from this link:

 3. Example of 'histfit' with normal distribution for GPS residuals.








Thursday, 4 December 2014

How to have all the functions in a single file, instead of having individual .m file for each function, in Matlab?

1. It is easy. It can be done by using functions-even the top level script should also be written as a function. An example is given below:

All these functions are present in Top.m

function Top
a = GetValue(1);
b = GetValue(2);
s = Sum2Num(a,b)
end

function val = GetValue(n)
val = n;
end


function s = Sum2Num(a,b)
s = a + b;
end


How to read a formatted text (ASCII) file in Matlab?

1. If a text (ASCII) file contains data with different data types then 'load' command is not useful. Then, one option can be to use TEXTSCAN with the format specifier as shown below.


fid = fopen(fileName)
formatSpec = '%f %s %f %f'

data = textscan(fid,formatSpec,3600*1,'Delimiter',',','CollectOutput', true);

fclose(fid)

The output will be a cell array. Each cell contains each of the elements. If you...
CollectOutput - will collect the consecutive data of same type in a single cell array.
Delimiter - lets you specify the delimiter used in the text file. If do not use the 'Delimiter' option then space is treated as the delimiter.


2. Many examples are given in the Matlab link given above: