Thursday, 10 September 2015

How to add a name to Matlab figure's title bar?

1. How to add a name to the Matlab figure's title bar? For example, Figure 1: Time of the year vs. Temperature.

figNum = 1;
figure(figNum)
set(figNum, 'Name', 'Time of the year vs. Temperature');


Output for this code is shown below:





Thursday, 5 March 2015

How to change default settings for figures in Matlab?

1. Use the following commands


set(0,'DefaultTextFontsize',8, ...
'DefaultTextFontname','Times New Roman', ...
'DefaultTextFontWeight','bold', ...
'DefaultAxesFontsize',8, ...
'DefaultAxesFontname','Times New Roman', ...
'DefaultLineLineWidth', 2);

Wednesday, 11 February 2015

How to reduce the gap (space) between subplots in Matlab?

1. tight_subplot  is a very good function which does exactly the same and gives us access to control the space between subplots.

Example:

figure(140);clf

%Units are normalized to 1. 
gap = [0.005 0.005]; %lower, uppper between subplots
figureMargin_topAndBottom = [.2 .15];
figureMargin_leftAndirght = [.2 .15];


ha = tight_subplot(2,2,gap, figureMargin_topAndBottom, figureMargin_leftAndirght)

corrMs = randn(100);

for ii = 1:1:4

     axes(ha(ii)), % this is important.... this activates that particular subplot
     surf(abs(corrMs));  shading interp
        view(0,0); % viewing from x-dimension (x-z axes)
     if ii > 2
        view(90,0); % just viewing from another dimension (y-z axes)
    end
end


set(ha(2),'ZTickLabel',[]);
set(ha(4),'ZTickLabel',[]);
set(ha(1:2),'YTickLabel',[]);
set(ha(3:4),'XTickLabel',[]);

set(gcf, 'Color', 'w'); % background of the figure, outside the axes

axes(ha(3)); % this activates the axes and lets you add the attributes to that subplot
ylabel('X Data');
zlabel('Z Data');



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: