Title: Matlab notes
Author: Darin Brezeale
Date Created: 05-01-2005
Updated: Thursday, 19-Jul-2007 23:12:02 EDT

Matrix Operations

count how many elements of a matrix are non-zero

diff_count = sum(some_matrix(:)~=0);

generate vector of 1024 from 0.0 to 1.0

x = linspace(0,1,1024);

use cell arrays for storing objects of different types (note the use of curly braces)

filenames = {'user911-noclass-HSV.csv',
             'user911-noclass-RGB.csv',
             'user3618-noclass-HSV.csv',
             'user3618-noclass-RGB.csv'}; 
[rows, cols] = size(filenames);
for i=1:rows
    username = filenames{i}; % note the use of curly braces
    userdata = csvread(username);
    gaptest2(userdata, linktype, distmeas, username)
end


Strings

get the first token from the string delimited by a dash (-)

username = strtok(userfile,'-');

concatenate string1 and string2

newname = strcat(string1, string2);


Figures

automatically dock figures

set(0,'DefaultFigureWindowStyle','docked')
close all figures
close all

force figure to be drawn now

drawnow

set the name of the figure to 'username' and turn the number off

figure('Name', username, 'NumberTitle', 'Off')

save the current figure to output.jpg in the jpg format

saveas(gcf, 'output.jpg', 'jpg');



File I/O

add directory to the front of the search path

path('Q:\ClusterValidityAnalysisPlatform3.42',path)

read image

c = imread('someimage.tif');

load file of comma-delimited values

data = load('somefile.csv');

load file of comma-delimited values beginning at row 1, column 2 (upper left of file is 0,0)

data = csvread('somefile.csv', 1, 2); % csvread expects only numeric values

save data to a file

a = [1 12 7897 342];

fp = fopen('testdata.txt', 'w'); for i=1:length(a) fprintf(fp, '% d', a(i)); end fclose(fp);

read and display all files with the extension .bmp

files = dir(fullfile('Q:\phd\wavelets\', '*.bmp'));
for n=1:numel(files)
    cfile=files(n).name;
	c123 = imread(cfile);
	figure, imshow(c123);
end

write matrix newarray to a file with the a comma as the delimiter (the default)

dlmwrite('output.txt',newarray)


Miscellaneous

remove items from the Workspace

clear

show variables in the Workspace

whos

References

  1. Mastering Matlab 7