Thursday 19 December 2013

How to set the size of the texts and axes indices in Matlab plot?

0.8. How to check the properties that are accessible to the user?

set(gcf) % this command lists all the properties

0.9. Setting default font name for axis and texts in figure


set(0,'defaultAxesFontName', 'arial')
set(0,'defaultTextFontName', 'arial')


1. To set the properties of ALL that have the property 'fontsize' in figure, use:

set(findall(gcf, '-property', 'FontSize'), 'FontSize', 12, 'fontWeight', 'normal')

% findall returns all the children which have the property 'fontsize'.
% This command will set size of the labels, legends, title, and axis indices

2. To set only the text part (this excludes the numbers (indices))

set(findall(gcf,'Type','text'),'FontSize',14);

%How to change the font name

set(findall(gcf,'Type','text'),'FontName','Arial');
%or
set(findall(gcf,'Type','text'),'FontName','Times New Roman');  


3. To set the line width of all the 'lines' in a plot

set(findall(gcf,'Type','line'),'linewidth',2)

3.1 To set the MarkerSize, MarkerEdgeColor, MarkerFaceColor in a plot
    set(findall(gcf,'Type','line'),'MarkerSize',10)
    set(findall(gcf,'Type','line'),'MarkerEdgeColor','r')
    set(findall(gcf,'Type','line'),'MarkerFaceColor','b')


%Above commands work for subplots as well

4. To control only the axes indices and legend when we have subplots

set(findall(gcf,'type','axes'),'fontsize',20);
%or
axes('FontSize',12)


% How to reduce the space between the subplots? 
Explanation is given in this link.
Example is given below:

h2 = subplot(312), plot(freqBins,acfDelay,'-o')

p  = get(h2, 'pos'); % returns [left, bottom, width, height] = [Pos(x,y), size(x,y)]

ylim([-30 40]);
set(gca,'ytick',[-30:15:30])
box off;
ylabel('Delay (m)');
set(gca,'xticklabel','')
grid on

p(2) = p(2) + 0.05;
set(h2, 'pos', p);



5. To apply these common settings to multiple figures:
When there are multiple figures, instead of having these commands for every plot, after generating all the plots use 'for loop' to apply these common settings to all the figures.

Use 'figure(n)' to select the figure to which the settings should be applied. This reduces the code size significantly and allows to easily modify all the figures at one place.



Example:

%% Beautifying figures


%To set the default font for texts and axes in figures.
set(0,'defaultAxesFontName', 'arial')
set(0,'defaultTextFontName', 'arial')

for figNum = 1:1:2
    figure(figNum);
    %To set the axes indices font
    set(findall(gcf,'type','axes'),'fontsize',12);

    %To set the line width
    set(findall(gcf,'Type','line'),'linewidth',2)



%To set the MarkerSize, MarkerEdgeColor, MarkerFaceColor in a plot
    set(findall(gcf,'Type','line'),'MarkerSize',10)
    set(findall(gcf,'Type','line'),'MarkerEdgeColor','r')
    set(findall(gcf,'Type','line'),'MarkerFaceColor','b')


    %To set the fontsize of the text inside the plot (legend and other text)
    set(findall(gcf,'Type','text'),'FontSize',12);
 
    %Set the Font name
    set(findall(gcf,'Type','text'),'FontName','arial');
 
    %To adjust size of the figure
    set(gcf,'Units','centimeters');
    figSize = [20 15]; %width x height
    set(gcf,'Position',[5 5 figSize]); % [position , size]
 
    %Adding legends - split into two columns. 'columnlegend' is from
    %FileExchange
    columnlegend(2, legArray,'location','northEast','boxoff') ; % previous set commands remove this effect. %Hence, used after those
 
    box off;
    grid on;
 
    %Saving the figure in meta file. This format allows us to edit the
    %figure in MS-word.
    saveas(gcf,'test1454523.emf','emf')
end

Saturday 7 December 2013

How to read from and write to a binary and text (ASCII) file in Matlab?

1. Here is the example for binary:

% Clear screen, clear workspace, and close all figures
clc;
clear all;
close all;

% Path and file name
% Read file
fReadName = 'ReadDataFile.bin'; % file from the current working directory

% Write file
fWriteName = 'WriteDataFile.bin';  % file to be written into the current working directory

% Open the file in read mode
 fidRead = fopen(fReadName,'rb');

 % Open the file in write mode
fidWrite = fopen(fWriteName,'wb');

% Read the data of correct data-type
dataType = 'int16';
% For more details on the Matlab supported data types: click here

numberOfSeconds = 300;
fSamp = 12.5e6;

tic
for ii = 1:1:numberOfSeconds

    numberOfwordsToReadPerTime = fSamp * 1 * 2;

   % Read from File
   dataFromFile = fread(fidRead,numberOfwordsToReadPerTime,dataType);

   % Write into the file
   fwrite(fidWrite,dataFromFile,dataType);

end

toc; % tic and toc gives the time taken by Matlab to complete the execution of the above for-loop.

% Close the files
fclose(fidRead);
fclose(fidWrite);

% 2a. Example for ASCII/text file containing numbers

a = magic(10); % generates 10x10 matrix of numbers
save bFile.txt a -ascii;

fid = fopen('bFile.txt','r');
d = fscanf(fid,'%f',20)  % just reads first 20 words into a column
fclose(fid);

%Skipping few number of words from the file requires exact number of bytes + %bytes for end-of-line.
% we can check the number of bytes read so far using the command 'ftell'


% 2b. Example for ASCII/text file containing numbers
%To read the 10x10 matrix
rowStart = 0;
rowColumn = 0;
rowEnd = 9;
colEnd = 9;

d2 = dlmread('bFile.txt','',[rowStart rowColumn rowEnd colEnd])



%Result


d =

    92
    99
     1
     8
    15
    67
    74
    51
    58
    40
    98
    80
     7
    14
    16
    73
    55
    57
    64
    41


d2 =

    92    99     1     8    15    67    74    51    58    40
    98    80     7    14    16    73    55    57    64    41
     4    81    88    20    22    54    56    63    70    47
    85    87    19    21     3    60    62    69    71    28
    86    93    25     2     9    61    68    75    52    34
    17    24    76    83    90    42    49    26    33    65
    23     5    82    89    91    48    30    32    39    66
    79     6    13    95    97    29    31    38    45    72
    10    12    94    96    78    35    37    44    46    53
    11    18   100    77    84    36    43    50    27    59

How to load the Matlab figure and extract data from the figure?

% To load figure

h = hgload('RectangularChipShape.fig')


get(h) % lists all the properties linked to this class

set(h) % lists all the properties that can be configured


Few examples:

set(gca,'fontsize',26);

hline = findobj(gcf, 'type', 'line');
hData = findobj(gcf, 'type', 'data');
set(hline,'LineWidth',4)
set(hline,'LineStyle',':')

idx = [4 5];
set(hline(idx),'Marker','*')


%To extract the data from the figure

axesObjs = get(h, 'Children'); %axes handles
dataObjs = get(axesObjs, 'Children'); %handles to low-level graphics objects in axes


objTypes = get(dataObjs{2});
xdata = get(dataObjs{2}, 'XData'); %data from low-level grahics objects
ydata = get(dataObjs{2}, 'YData');

%To extract data from Surf/Mesh plot
m = magic(10);

figure; h1 = mesh(m); % works for 'surf' also

h12 =gcf; % returns the figure number
 
h1Children = get(h12,'Children');
h1Chotas = get(h1Children,'Children'); % this is equal to h1
xD = get(h1Chotas,'xdata')
yD = get(h1Chotas,'ydata')
zD = get(h1Chotas,'zdata'


% Detailed explanation is available at:

http://www.mathworks.com/help/matlab/learn_matlab/understanding-handle-graphics-objects.html

Thursday 5 December 2013

What are 'cells' in Matlab?

Cells = Arrays that can contain data of varying types and sizes.

More details at:

http://www.mathworks.com/help/matlab/cell-arrays.html

How to create 'structures' in Matlab?

Example: Following command creates a structure with various fields with different data types.

struct('ifData', zeros(1, 1000), 'fSamp', 10e6, 'fif',0.42e6, 'noOfBits',16,'complexOrReal','complex','prn',10)



>> struct('ifData', zeros(1, 1000), 'fSamp', 10e6, 'fif',0.42e6, 'noOfBits',16,'complexOrReal','complex','prn',10)

ans =
           ifData: [1x1000 double]
            fSamp: 10000000
              fif: 420000
         noOfBits: 16
    complexOrReal: 'complex'
              prn: 10


 

How to include folders into Matlab library search?

If we want to call some Matlab functions which are there in a separate folder (these files are not in the current working directory), then we can use 'addpath' command add that folder to library-search folders.

Example: If 'Acquisition' is a folder inside the working directory, then:

addpath Acqusition;

 %will add the folder and we can directly call the functions that are inside %'Acquisition' folder.

or, we can give the full absolute path of the folder:

addpath F:\Users\Naveen\UofC\638\Lab2\Work_code\Acqusition;