Thursday, 18 December 2014
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.
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
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:
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:
Friday, 21 November 2014
How to stop Matlab at the point of error for debugging?
1. There is an useful command 'dbstop'. Check help for more details.
2. To stop the control at the point of error in part of the function or the script use
dbstop if error
3. This has to be only once in the file. It gets applied to all the files.
4. Quite useful. Avoids rerunning the script to look for the reasons for the errors. It stops the control at the line where error is encountered and retains the work-space variables that belong to that function.
2. To stop the control at the point of error in part of the function or the script use
dbstop if error
3. This has to be only once in the file. It gets applied to all the files.
4. Quite useful. Avoids rerunning the script to look for the reasons for the errors. It stops the control at the line where error is encountered and retains the work-space variables that belong to that function.
Thursday, 13 November 2014
How to design a simple low pass filter (LPF) in Matlab?
%% LPF filter specifications
fSamp = 45e6;
Fpass = 1.25e6/(fSamp/2); % Passband Frequency
Fstop = 2.5e6/(fSamp/2); % Stopband Frequency
Apass = 1; % Passband Ripple (dB)
Astop = 80; % Stopband Attenuation (dB)
match = 'passband'; % Band to match exactly
% Construct an FDESIGN object and call its BUTTER method.
%d = fdesign.lowpass(Fpass, Fstop, Apass, Astop, fSamp);
d=fdesign.lowpass('Fp,Fst,Ap,Ast',Fpass,Fstop,Apass,Astop);
designmethods(d)
Hd = design(d,'equiripple');
fvtool(Hd); %shows the response
%Using filter
inpSignalFiltered = filter(Hd, inpSignal);
fSamp = 45e6;
Fpass = 1.25e6/(fSamp/2); % Passband Frequency
Fstop = 2.5e6/(fSamp/2); % Stopband Frequency
Apass = 1; % Passband Ripple (dB)
Astop = 80; % Stopband Attenuation (dB)
match = 'passband'; % Band to match exactly
% Construct an FDESIGN object and call its BUTTER method.
%d = fdesign.lowpass(Fpass, Fstop, Apass, Astop, fSamp);
d=fdesign.lowpass('Fp,Fst,Ap,Ast',Fpass,Fstop,Apass,Astop);
designmethods(d)
Hd = design(d,'equiripple');
fvtool(Hd); %shows the response
%Using filter
inpSignalFiltered = filter(Hd, inpSignal);
Wednesday, 6 August 2014
How to add colorbar ( adjust its size and label) to a plot in Matlab?
1. Use 'colorbar'
c=colorbar('vert'); % vert adds colorbar to z-axis values. 'horiz' adds color bar to x-values.
2. Reduce the size of the colorbar
xa1=get(gca,'position');
xa=get(c,'Position');
xa(3)=0.03;
xa(4)=0.65;
set(c,'Position',xa)
3. Adding title to the colorbar (this sits on top)
title(c,'Hz')
4. Adding ylabel to it (along vertical axis)
ylabel('Error')
Example:
c=colorbar('vert'); % vert adds colorbar to z-axis values. 'horiz' adds color bar to x-values.
2. Reduce the size of the colorbar
xa1=get(gca,'position');
xa=get(c,'Position');
xa(3)=0.03;
xa(4)=0.65;
set(c,'Position',xa)
3. Adding title to the colorbar (this sits on top)
title(c,'Hz')
4. Adding ylabel to it (along vertical axis)
ylabel('Error')
Example:
Subscribe to:
Posts (Atom)



