Site

Matlab

file IO

read data

This page has some good examples:

http://www.mathworks.com/help/techdoc/ref/fscanf.html

For example, if the file is

1 2
2 3
3 4

We can use

fid = fopen('filename');
A = fscanf(fid, '%g %g',[2 inf]);
then
a = [
1 2 3;
2 3 4;
]

open file

output = [output_dir int2str(s_base_year) m_base_str sd_base_str ed_base_str '.' keyword '.' int2str(tunit) '.' strtrim(level(l, :)) '.eigen.txt'];
fd = fopen(output,'w');
fprintf(fd,'%d ', eigenvalue(i,j));
fclose(fd);

plot

plot(1:(num_week*cycle_num), entro(1:(num_week*cycle_num)), '-',...
       1:(num_week*cycle_num), cp_vector, 'ro',...
       1:(num_week*cycle_num), topic_X_vector, '-*');	
[cur_ylim] = get(gca, 'YLim')
cur_ylim(1) = 0;
set(gca, 'YLim', cur_ylim);
print([this_output_file '.pdf'],'-dpdf');
figure;
hl1 = line(1:(num_week*cycle_num), topic_X_vector, 'Color','b', 'Marker', '.');
hl2 = line(1:(num_week*cycle_num), cp_vector, 'Color', 'r', 'Marker', '.', 'MarkerFaceColor', 'r', 'LineStyle', 'none');
ax1 = gca;
[cur_ylim] = get(ax1, 'YLim');
cur_ylim(1) = 0;
set(ax1, 'YLim', cur_ylim);
ax2 = axes('Position',get(ax1,'Position'),...
		'XLim', get(ax1, 'XLim'),...
		'XAxisLocation','bottom',...
		'YAxisLocation','right',...
		'Color','none',...
		'XColor','k','YColor','k');
hl3 = line(1:(num_week*cycle_num), entro(1:num_week*cycle_num), 'Color', 'g', 'Parent', ax2);
figure('visible','off')
xlabel('Time')
ylabel('Count')
x_ticks = base_date + 1 * (tunit / 86400) + ([1:time] - 1) .* (tunit / 86400);
x_show_ticks = base_date + ([1:(35 * 86400 / tunit):time] - 1) .* (tunit / 86400);
ax1 = gca;
set(ax1,'XTick', x_show_ticks)
datetick(ax1,'x','yy-mm-dd','keepticks','keepticks')
if(twitter_weekly == 0)
	ax2 = axes('Position',get(ax1,'Position'),...
			'XLim', get(ax1, 'XLim'),...
			'XAxisLocation','bottom',...
			'YAxisLocation','right',...
			'Color','none',...
			'XColor','k','YColor','k');
	set(ax2,'XTick', x_show_ticks)
	datetick(ax2,'x','yy-mm-dd','keepticks','keepticks')
	hl1 = line(x_ticks, Cate_X, 'Color','b', 'Parent', ax1);
	hl2 = line(x_ticks, Twitter_X, 'Color','r', 'Parent', ax2);
	ylabel(ax1, 'Clarify Volume')
	ylabel('Tweet Volume')
else
	hl1 = line(x_ticks, Cate_X, 'Color','b');
	hl2 = line(x_ticks, Twitter_X, 'Color','r');
end
legend(ax1, [hl1; hl2], ['category'; 'twitter '])
% print(['figures/' name '.pdf'],'-dpdf');
print([out_figure_dir '/' name '.jpeg'],'-djpeg');
close all

Font Size

set(gca,'FontSize', 24, 'fontWeight', 'bold')
set(findall(gcf,'type','text'), 'FontSize', 24, 'fontWeight', 'bold')

PCA

another way to do PCA from web site

% FileName is the name of the file to work on, OutputSize is no. of
% dimensions to output after PCA
function [output_args]=ReduceUsingPCA(DirName,OutputSize)
	files = dir(fullfile(DirName, ‘*.csv’));
	for i=1:length(files)
		% read files(i).name and process
		FileName= [DirName '/' files(i).name];
		% read in csv file from FileName and store in x
		x = csvread(FileName);
		[Rows, Columns] = size(x);  % find size of input matrix
		m=mean(x);                  % find mean of input matrix
		y=x-ones(size(x,1),1)*m;    % normalise by subtracting mean
		c=cov(y);                   % find covariance matrix
		[V,D]=eig(c);               % find eigenvectors (V) and eigenvalues (D) of covariance matrix
		[D,idx] = sort(diag(D));    % sort eigenvalues in descending order by first diagonalising eigenvalue matrix, idx stores order to use when ordering eigenvectors
		D = D(end:-1:1)’;
		V = V(:,idx(end:-1:1));     % put eigenvectors in order to correspond with eigenvalues
		V2d=V(:,1:OutputSize);        % (significant Principal Components we use, OutputSize is input variable)
		prefinal=V2d’*y’;
		final=prefinal’;            % final is normalised data projected onto eigenspace
		[infile, remain] = strtok(FileName,’/');
		infile = strtok(remain,’.');
		mkdir([num2str(OutputSize) 'PC']);
		outputfilename = [num2str(OutputSize) 'PC' infile '_' num2str(OutputSize) 'PCs.csv'];
		csvwrite(outputfilename,final);
	end
end

Hypothesis Tests

All available Hypothese tests in Matlab are here.