MATLAB sumup

UI

Dialog box

Refer to: matlab GUI之常用对话框

Get rect from mouse

1
2
imshow('test.jpg');
rect = getrect;

I/O

Get user input

  • Via command window
    1
    strResponse = input('Do you want more? Y/N [Y]: ', 's');
  • Via UI
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    prompt = { 'from:','to:','step'};
    dlg_title = 'choose images';
    num_lines = [4 1 3]; % can be a scaler number that applies to all
    defAns = {'0', '', '1'};
    options = 'on'; % enable to resize window
    answer = inputdlg(prompt,dlg_title,num_lines,defAns);

    from=str2num(cell2mat(answer(1)));
    to=str2num(cell2mat(answer(2)));
    stepsize=str2num(cell2mat(answer(3)));

Ouput to TXT

1
2
3
fileID = fopen('faces.txt', 'w');
fprintf(fileID,'[%4.2f,%d,%d,%d]\n', x, y, z, t);
fclose(fileID);

File

Get diff parts from path (e.g., folder, filename and extension)

1
[pathstr,realname,ext] = fileparts('C:\\images\\test.bmp');

=>

1
2
3
pathstr = 'C:\images\'
realname = 'test'
ext = '.bmp'

Get filename via UI

1
2
[filename, pathname] = uigetfile({'*.*';'*.avi';'*.mpg';'*.wmv';'*.asf';'*.asx';'*.mj2'}, 'Select a VIDEO file...');
video_file_name = fullfile(pathname, filename); % use fullfile for OS independent

Get folder via UI

1
output_images_folder = uigetdir(pwd, 'Select the FOLDER where to save images...')

Iterate through all files in folder

1
2
3
4
5
6
7
8
data_pwd = uigetdir;
file_temp = sprintf('%s\\*.%s', data_pwd, input_type);
filearray = dir(file_temp);
s=max(size(filearray));
for i=1:1:s
imgname=strcat(data_pwd,'\\',filearray(i).name);
... ...
end

Iterate through all frames of a video

1
2
3
4
5
mov = VideoReader(video_file_name);
for i=1:1:mov.numberofframes
b=read(mov,i);
... ...
end

Misc

Get image height/width/channel

1
2
img = imread('C:\Users\tommyhu\Desktop\test\frame_0000.bmp');
[h, w, c] = size(img);

Functions with required and optional inputs

Refer to http://www.mathworks.com/help/matlab/matlab_prog/parse-function-inputs.html.

MEX with multiple include paths 1

You need to use two different variables or a cell array of paths:

1
2
3
path1 = ['-I' fullfile(pwd, 'tempInclude1')];
path2 = ['-I' fullfile(pwd, 'tempInclude2')];
mex('-v', path1, path2, srcFile)

or

1
2
ipaths = {['-I' fullfile(pwd,'tempInclude1')], ['-I' fullfile(pwd, 'tempInclude2')];}
mex('-v', ipaths{:}, srcFile)

String catenation

  1. sprintf
    1
    file_temp = sprintf('%s\\*.%s', data_pwd, input_type);
  2. strcat
    1
    imgname = strcat(data_pwd,'\\', filearray(i).name);
1. Multiple include paths in mex