## Turning y axis upside down in MATLAB

The ‘YDir’ axes property can be either ‘normal’ or ‘reverse’. By default it is ‘normal’ for most plots, but some plots will automatically change it to ‘reverse’, such as the image or imagesc functions. You can set the y-axis direction of an axes with either the set function or dot indexing (in newer MATLAB versions): … Read more

## Automatically maximize a figure

This worked for me: figure(‘units’,’normalized’,’outerposition’,[0 0 1 1]) or for current figure: set(gcf,’units’,’normalized’,’outerposition’,[0 0 1 1]) I have also used MAXIMIZE function on FileExchange that uses java. This is true maximization.

## MATLAB Compiler vs MATLAB Coder

MATLAB Compiler encrypts and archives your MATLAB code (which remains as MATLAB .m code), and packages it in a thin executable (either .exe or .dll) wrapper. This is delivered to the end user along with the MATLAB Compiler Runtime (MCR). If you wish, the MCR can be packaged within the executable as well. The MCR … Read more

## How to initialize an array of structs in MATLAB?

Using repmat is by far the most efficient way to preallocate structs : N = 10000; b = repmat(struct(‘x’,1), N, 1 ); This is ~10x faster using Matlab 2011a than preallocating via indexing, as in N = 10000; b(N).x = 1 The indexing method is only marginally faster than not preallocating. No preallocation: 0.075524 Preallocate … Read more

## How to use SIFT algorithm to compute how similar two images are?

First, aren’t you supposed to be using vl_sift instead of sift? Second, you can use SIFT feature matching to find correspondences in the two images. Here’s some sample code: I = imread(‘p1.jpg’); J = imread(‘p2.jpg’); I = single(rgb2gray(I)); % Conversion to single is recommended J = single(rgb2gray(J)); % in the documentation [F1 D1] = vl_sift(I); … Read more

## How do I get the handles of all open figures in MATLAB

There are a few ways to do this. One way to do this is to get all the children of the root object (represented in prior versions by the handle 0): figHandles = get(groot, ‘Children’); % Since version R2014b figHandles = get(0, ‘Children’); % Earlier versions Or you could use the function findobj: figHandles = … Read more

## Octave/Matlab: Adding new elements to a vector

x(end+1) = newElem is a bit more robust. x = [x newElem] will only work if x is a row-vector, if it is a column vector x = [x; newElem] should be used. x(end+1) = newElem, however, works for both row- and column-vectors. In general though, growing vectors should be avoided. If you do this … Read more

## String to variable name MATLAB

Use eval(): var = eval(strcat(‘x’,’a’)); It will “evaluate” the string ‘xa’ and translate it to the value of the variable xa. Source : MATLAB documentation

## how to count unique elements of a cell in matlab?

To count unique elements, you can combine UNIQUE with ACCUMARRAY c = {‘a’, ‘b’, ‘c’, ‘a’}; [uniqueC,~,idx] = unique(c); %# uniqueC are unique entries in c %# replace the tilde with ‘dummy’ if pre-R2008a counts = accumarray(idx(:),1,[],@sum); To produce the structure, use NUM2CELL and STRUCT: countCell = num2cell(counts); tmp = [uniqueC;countCell’]; %’ unique_count = struct(tmp{:}) … Read more

## How to draw horizontal and vertical lines in MATLAB?

MATLAB’s plotting works on a point-by-point basis from the vectors you give. So to create a horizontal line, you need to varying x while keeping y constant and vice-versa for vertical lines: xh = [0,10]; yh = [245,245]; % constant xv = [5,5]; % constant yv = [0,245*2]; plot(xh,yh,xv,yv);