Matlab Cheatsheet

Printable Online Cheat Sheet

Choose color for Matlab plot


%------------- This is Comment------------------%

clear variables;

close all ;

clc;

% comment out block

%{

stuff to be commented out

%}


% display the value of "n"

disp(n);

% special characters


%Column Sum:

S=sum(A,1)

% Row Sum:

S=sum(A,2)


%Replace value in a matrix:

A=[9,6,3;6,4,2;3,2,1];

A(2,2)=100;


%Divide Matrix by the same row:

A=[9,6,3;4,2,8;1,0,0];

B=[3,2,1];

bsxfun(@rdivide,A,B(:));


%Rpeat column:

Li_agri=repmat(labor(:,8,1),1,T);

Li_agri(:,11:end)=repmat(Li_agri(:,10),1,T-10);


% fminsearch with bounds

%%%% About plot %%%%%%%%%%%%

figure(91)

upper=1.1*max(max(Tl_nonagri(1,:))) ;

lower=0.9*min(min(Tl_agri(2,:))) ;

patch([1985 1990 1990 1985], [lower lower upper upper], [0.9 0.9 0.9], 'edgecolor', 'none'); hold on

patch([1995 2005 2005 1995], [lower lower upper upper], [0.8 0.8 0.8], 'edgecolor', 'none'); hold on

patch([2005 2010 2010 2005], [lower lower upper upper], [0.9 0.9 0.9], 'edgecolor', 'none'); hold on

p1 = plot(year,Tl_agri(1,:),'b','LineWidth',2); hold on

p2 = plot(year,Tl_agri(2,:),'b--','LineWidth',2); hold on

p3 = plot(year,Tl_nonagri(1,:),'r','LineWidth',2); hold on

p4 = plot(year,Tl_nonagri(2,:),'r--','LineWidth',2); hold on

plot(year, ones(1,T),'k');

axis([1978 2010 lower upper]);

title('Labor Wedges (Marginal Product of Labor)','FontSize',14);

legend([p1 p2 p3 p4],{'Agri, Coastal','Agri, Inland', 'NonAgri, Coastal','NonAgri, Inland'},'Location','Best','FontSize',18);

saveas(gcf,fullfile(GRAPHOUTPUT, 'tau_L'), 'png');

close



%Box plot:

figure(2)

subplot(2,1,1);

boxplot(Ai_agri, year);

title('TFP Agri');


subplot(2,1,2);

boxplot(Ai_nonagri, year);

title('TFP NonAgri');

text(1990, 0.5, 'US history', 'Color', [0.9290 0.6940 0.1250],'FontSize',16);

saveas(gcf,'TFP by sector.png')

% Alternatively, can define the export folder

OUTPUT='/Users/tongtong/Attempt4.NegativeDistortion';

saveas(gcf,fullfile(OUTPUT, 'Allocation_True_i'), 'png');


%Multiple plots on one graph

figure

subplot(2,2,1) % add first plot in 2 x 2 grid

plot(x,y1) % line plot

title('Subplot 1')


subplot(2,2,2) % add second plot in 2 x 2 grid

scatter(x,y2) % scatter plot

title('Subplot 2')


subplot(2,2,3) % add third plot in 2 x 2 grid

stem(x,y1) % stem plot

title('Subplot 3')


subplot(2,2,4) % add fourth plot in 2 x 2 grid

yyaxis left % plot against left y-axis

plot(x,y1)

yyaxis right % plot against right y-axis

plot(x,y2)

title('Subplot 4')