Skip to main content

Matlab

Matlab Tricks



1. Save figure in high resolution
H=figure(1)
sc_sz = get(0,'ScreenSize');
set(H, ...
    'PaperPositionMode', 'manual', ...
    'PaperUnits', 'inches', ...
    'PaperPosition', [0 0 sc_sz(3)/100 sc_sz(4)/100])
print(H, '-dpng', '-r1000');
or
save EMF ( Enhanced metafile)  format



2. Multiple Boxplots
index= 1;
for i= 1: length(data(:,1))
    tempdata= data{i,1}(:,5); %data to be plotted
    tempdata = tempdata (~isnan(tempdata));
    for j = 1 : length(tempdata)
        matToPlot(index,1) = tempdata(j);
        groupVector(index,1) = i;
        index = index+1;
    end
end
boxplot(matToPlot,groupVector,'colors',[0.6 0.6 0.6]);
h=findobj(gca,'tag','Outliers');
delete(h)%outlier delete




3. Put text in image
gtext('Cauvery')



4. Insert a color background layer
p=patch([0 40 40 0],[151 151 273  273],'k');
set(p,'FaceAlpha',0.05)
set(p,'EdgeAlpha',0.3)
hold on




5. Plot Timeseries
colour_teal=[20 239 144] ./ 255;
plot(1:lengt,keep_ori,'Color',colour_teal,'LineStyle','-','LineWidth',1.5,...
 'Marker','o', 'MarkerSize',2);
choose colour here




6. Save large data
save('India_data','India_data','-v7.3');



7. Plot Design
set(gca,'Xtick',[1,2,3,4])
ylim([1 366])
xlim([0 34])
set(gca,'YTick',[1,31,59,90,120,151,181,212,243,273,304,334,366])
set(gca,'ticklength',0.5*get(gca,'ticklength'))
set(gca,'XTick',([]))



8. Set Legend Properties
hLegend = legend(LegHandles,LegText,'Orientation', 'vertical', 'FontSize', 9, 'Location', 'northeast');
set(hLegend,'Interpreter','none','FontSize',7.5);%changes legend size also
set( hLegend,'color','none'); %transparent background
legend boxoff % transparent background + no box
%precise position of legends
leg_pos = get(hLegend,'position') ;
set(hLegend,'position',[leg_pos(1)+0.2*leg_pos(3),leg_pos(2),...
                       leg_pos(3)*0.8,leg_pos(4)]) ;



9. Save figures in loop



for i=1:32
h=figure(i);
Data=AM(:,i+1);
time=AM(:,1);
plot(time,Data,'LineStyle','-','LineWidth',1.5,...
 'Marker','o''MarkerSize',2);
saveas(h,sprintf('CDF%d.png',i)
end






10. Remove Zero value rows

New_DATA = DATA(DATA(:,1)~=0,:);
New_DATA = DATA(~isnan(DATA));




11. Some Random Number Generation

m = datasample(1:100,5,'Replace',false)'; % Randomly select without replacement ex: Upper Limit=100; data required=5
n = repelem(1:5,RV); % Repeated Number generation ex: RV=[1,1,2,1,1] Vector of Repeatation No  3 twice
x = n(randperm(length(n))); % Random suffling
Cel = randi([1, size(x,2)-1],1,1); % A random cell position in a row
y = reshape(x,[2,length(x)/2])'; % Reshape any matrix
ran = (1-(-1)).*rand(1,1)-1; % random no in [1,-1] (UL-LL).rand(row,col)-UL
InterD = pdist2(pointVec,centerVec,'euclidean')'; % ||pointVec-centerVec||





12. Some 3D Plotting

Plotting 1
scrsz = get(groot,'ScreenSize');figure('Position',scrsz);M=zeros(20,15);
for i=1:20
    N=rand(1,15);
    M(i,:)=N;
    surf(M);az =72;el =82; view(az, el);hold on;
    title(sprintf('Time %i seconds',i));
    xlabel('xSpace');ylabel('ySpace');zlabel('Height');
    pause(0.15);getframe(gcf);
end
hold off;surf(M);az =72;el =82; view(az, el);colorbar('AxisLocation','in')
Plotting 2

pause(2);mesh(M);az =72;el =82; view(az, el)
pause(2);pcolor(M);xlabel('time');ylabel('space');title('Colour Map');colorbar('southoutside')
pause(2);contour(M);xlabel('Time');ylabel('Space');title('Contour');colorbar('southoutside')
pause(2);contourf(M);xlabel('Time');ylabel('Space');title('Colour Contour');colorbar('southoutside')











13. Avoid 'FOR LOOP'


Matrixwise Average Calculation | Data Generation
for j=1:5
    C{j,1}=rand(3,7);
end
% Calculation
ChangeDim=cat(3,C{:}); % Creating higher dimensional matrix
AMW=mean(ChangeDim,3); % Average Matrix Wise
Creating Matrix with row wise sum to 1
p=rand(7,3); % Data Generation
FM=bsxfun(@rdivide,p, sum(p,2)); % Calculation