I learned a lot of important things yesterday, after spend 10 hours in front of computer playing with MatLab. Alright, here's the code I came up with.
%Created on 6/30/2011
clear all
clc
sitenumber='01';
SampleLocation=[];
AER=[];
Err=[];
%% Importing data from intermediate file
filename='whatever it is.xlsx';
D1=xlsread(filename, 'A14:C25');
D2=xlsread(filename, 'I14:K25');
D3=xlsread(filename, 'Q14:S25');
D4=xlsread(filename, 'Y14:AA25');
D5=xlsread(filename, 'AG14:AI25');
D6=xlsread(filename, 'AO14:AQ25');
D7=xlsread(filename, 'AW14:AY25');
D8=xlsread(filename, 'BE14:BG25');
D9=xlsread(filename, 'BM14:BO25');
D10=xlsread(filename, 'BU14:BW25');
D11=xlsread(filename, 'CA14:CD25');
D12=xlsread(filename, 'CJ14:CL25');
D13=xlsread(filename, 'CR14:CT25');
D14=xlsread(filename, 'CZ14:DB25');
D15=xlsread(filename, 'DH14:DJ25');
D16=xlsread(filename, 'DP14:DR25');
D17=xlsread(filename, 'DX14:DZ25');
D18=xlsread(filename, 'EF14:EH25');
DCell={D1,D2,D3,D4,D5,D6,D7,D8,D9,D10,...
D11,D12,D13,D14,D15,D16,D17,D18}; % Put all the arrays into one big cell array so that each time I can take one cell out and use it as the function input.
% Use each array as function input % This is a self-defined function
for k=1:18
[AERate Error]=getDecayrate(DCell{k}); %If use DCell (k), the function won't work.
SampleLocation(1,k)=k;
AER(1,k)=AERate;
Err(1,k)=Error;
newfilename=strcat('Site_', sitenumber, '_SampleLocation_', num2str(k),'.fig');
saveas(gcf,newfilename) % This saveas function works pretty good. The .fig file can be edited later in MatLab.
end
Result=[SampleLocation', AER', Err'];
%The following function calculates the first-order decay rate of the tracer gas, and returns two parameters.
function [AER Err] = getDecayrate( location)
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
%AER=Data-1;
TimeS=zeros(1, length(location))';
LNC1=zeros(1, length(location))';
LNC2=zeros(1, length(location))';
for i=2 : length(location)
TimeS(i)=(location(i,1)-location(1,1))*1440/60;
end
for j=2 : length(location)
LNC1(j)=log(location(j,2)/location(1,2));
LNC2(j)=log(location(j,3)/location(1,3));
end
slope1=sum(TimeS.*LNC1)/sum(TimeS.^2);
slope2=sum(TimeS.*LNC2)/sum(TimeS.^2);
AER=abs((slope1+slope2)/2);
Err=abs(AER-abs(slope1));
TimeModel=0:0.01:max(TimeS);
LNC1Model=TimeModel.*slope1;
LNC2Model=TimeModel.*slope2;
plot(TimeS, LNC1, 'xr',TimeS, LNC2, 'sb', TimeModel, LNC1Model,'r', TimeModel, LNC2Model, 'b', 'MarkerSize',10, 'LineWidth', 2)
title ('Location')
xlabel('Time (hours)')
ylabel('Ln(C/C0)')
end
MATLAB applications, tutorials, examples, tricks, resources,...and a little bit of everything I learned ...
Friday, July 1, 2011
Wednesday, June 1, 2011
simple data matching by using dataset arrays
Task : There are two Excel files, one contains the sample number and the time of sampling while the other contains the analysis results of each sample. Because the samples were taken in a chronicle order and were analysed in random order, it is kind of time consuming to manually find out each sample's result and put them into one file, especially when dealing with thousands of samples.
Here's a short code I wrote to do this:
% This program search the bag numbers and put the corresponding
% concentration values into the data file.
%% initialization
clear all;
clc;
%% import data
dataf=dataset('xlsfile','back.xls'); %aisle files from field test
concf=dataset('xlsfile','concentration.xls'); %concentration readings from autoTrac
%% determine dataset array size
datafL=length(dataf);
concfL=length(concf);
%% search and match
for i=1 : datafL
for j= 1 : concfL-1
if ( char(dataf.BagNumber(i))==char(concf.BagNumber(j)) )
dataf.Concentration1(i)=concf.Concentration(j);
dataf.Concentration2(i)=concf.Concentration(j+1);
break
end
end
end
%% Output data
export(dataf, 'xlsfile', 'Processed bag files-back.xls')
The dataset array is very power at handling different type of data, including numbers, strings, and even matrix.
To write a dataset array to an Excel file, I need to use export function instead of xlswrite.
Here's a short code I wrote to do this:
% This program search the bag numbers and put the corresponding
% concentration values into the data file.
%% initialization
clear all;
clc;
%% import data
dataf=dataset('xlsfile','back.xls'); %aisle files from field test
concf=dataset('xlsfile','concentration.xls'); %concentration readings from autoTrac
%% determine dataset array size
datafL=length(dataf);
concfL=length(concf);
%% search and match
for i=1 : datafL
for j= 1 : concfL-1
if ( char(dataf.BagNumber(i))==char(concf.BagNumber(j)) )
dataf.Concentration1(i)=concf.Concentration(j);
dataf.Concentration2(i)=concf.Concentration(j+1);
break
end
end
end
%% Output data
export(dataf, 'xlsfile', 'Processed bag files-back.xls')
The dataset array is very power at handling different type of data, including numbers, strings, and even matrix.
To write a dataset array to an Excel file, I need to use export function instead of xlswrite.
Tuesday, March 15, 2011
How to insert a space in Mathtype equations
It took me a while to figure out how to insert a space in Mathtype equations. This is especially useful when you write an equation with multiplication of a few items, but you don't want to use a dot or a multiplication sign. Here is the trick:
Ctrl + Shift + space
Done!
Ctrl + Shift + space
Done!
Sunday, December 19, 2010
A Tai Chi wallpaper for your desktop
I learned photoshop CS2 recently. Here is a wallpaper I made. Please feel free to download it and use it as your desktop.
Wednesday, October 20, 2010
solve linear equation system
To solve a linear equations system:
x+y+z=3
2x+2y+z=5
4x+8y-3z=9
linsolve is the function to use.
a=[1 1 1; 2 2 1; 4 8 -3];
b=[3;5;9];
linsolve(a,b)
Done!
x+y+z=3
2x+2y+z=5
4x+8y-3z=9
linsolve is the function to use.
a=[1 1 1; 2 2 1; 4 8 -3];
b=[3;5;9];
linsolve(a,b)
Done!
Tuesday, October 19, 2010
roots function and solve functioin
Today I learned two methods to solve polynomial.Two functions, roots and solve can be used. For example, if I would like to solve the polynomial:
x^3+2*x^2-8*x+10=0
I can use:
r=roots([1 2 -8 10])
or:
solve('x^3+2*x^2-8*x+10')
However, the answers will be given in different form. The first one will give numerical values of the roots, like:
ans =
-4.3605
1.1803 + 0.9488i
1.1803 - 0.9488i
the latter one will give symbolic form answers:
ans =
-1/3*(215+3*2697^(1/2))^(1/3)-28/3/(215+3*2697^(1/2))^(1/3)-2/3
1/6*(215+3*2697^(1/2))^(1/3)+14/3/(215+3*2697^(1/2))^(1/3)-2/3+1/2*i*3^(1/2)*(-1/3*(215+3*2697^(1/2))^(1/3)+28/3/(215+3*2697^(1/2))^(1/3))
1/6*(215+3*2697^(1/2))^(1/3)+14/3/(215+3*2697^(1/2))^(1/3)-2/3-1/2*i*3^(1/2)*(-1/3*(215+3*2697^(1/2))^(1/3)+28/3/(215+3*2697^(1/2))^(1/3))
Thursday, October 7, 2010
I was using a lot of greek symbols in my Word document, and usually when I need one, I input a English letter, for example, "a", then highlight it and change the font to symbol. That way, I will have a "α" there. However, if someone changed the font of that letter, it won't show up correctly- a blank will be left there instead of the letter you want.
I did some research and found one way to solve this problem: Firstly type in the unicode for the greek letter, then press "Alt+X". The unicode will be turned into a greek letter, and changing the font will not cause any trouble. The greek letter will show up anyway, just look a little different when different font is used.
And here is the reference for the unicode. This is very cool!
0391 Α GREEK CAPITAL LETTER ALPHA
0392 Β GREEK CAPITAL LETTER BETA
0393 Γ GREEK CAPITAL LETTER GAMMA
0394 Δ GREEK CAPITAL LETTER DELTA
0395 Ε GREEK CAPITAL LETTER EPSILON
0396 Ζ GREEK CAPITAL LETTER ZETA
0397 Η GREEK CAPITAL LETTER ETA
0398 Θ GREEK CAPITAL LETTER THETA
0399 Ι GREEK CAPITAL LETTER IOTA
039A Κ GREEK CAPITAL LETTER KAPPA
039B Λ GREEK CAPITAL LETTER LAMDA
039C Μ GREEK CAPITAL LETTER MU
039D Ν GREEK CAPITAL LETTER NU
039E Ξ GREEK CAPITAL LETTER XI
039F Ο GREEK CAPITAL LETTER OMICRON
03A0 Π GREEK CAPITAL LETTER PI
03A1 Ρ GREEK CAPITAL LETTER RHO
03A3 Σ GREEK CAPITAL LETTER SIGMA
03A4 Τ GREEK CAPITAL LETTER TAU
03A5 Υ GREEK CAPITAL LETTER UPSILON
03A6 Φ GREEK CAPITAL LETTER PHI
03A7 Χ GREEK CAPITAL LETTER CHI
03A8 Ψ GREEK CAPITAL LETTER PSI
03A9 Ω GREEK CAPITAL LETTER OMEGA
03AA Ϊ GREEK CAPITAL LETTER IOTA WITH DIALYTIKA
03AB Ϋ GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA
03B1 α GREEK SMALL LETTER ALPHA
03B2 β GREEK SMALL LETTER BETA
03B3 γ GREEK SMALL LETTER GAMMA
03B4 δ GREEK SMALL LETTER DELTA
03B5 ε GREEK SMALL LETTER EPSILON
03B6 ζ GREEK SMALL LETTER ZETA
03B7 η GREEK SMALL LETTER ETA
03B8 θ GREEK SMALL LETTER THETA
03B9 ι GREEK SMALL LETTER IOTA
03BA κ GREEK SMALL LETTER KAPPA
03BB λ GREEK SMALL LETTER LAMDA
03BC μ GREEK SMALL LETTER MU
03BD ν GREEK SMALL LETTER NU
03BE ξ GREEK SMALL LETTER XI
03BF ο GREEK SMALL LETTER OMICRON
03C0 π GREEK SMALL LETTER PI
03C1 ρ GREEK SMALL LETTER RHO
03C2 ς GREEK SMALL LETTER FINAL SIGMA
03C3 σ GREEK SMALL LETTER SIGMA
03C4 τ GREEK SMALL LETTER TAU
03C5 υ GREEK SMALL LETTER UPSILON
03C6 φ GREEK SMALL LETTER PHI
03C7 χ GREEK SMALL LETTER CHI
03C8 ψ GREEK SMALL LETTER PSI
03C9 ω GREEK SMALL LETTER OMEGA
03D1 ϑ GREEK THETA SYMBOL
03D2 ϒ GREEK UPSILON WITH HOOK SYMBOL
03D5 ϕ GREEK PHI SYMBOL
03D6 ϖ GREEK PI SYMBOL
03DC Ϝ GREEK LETTER DIGAMMA
03F0 ϰ GREEK KAPPA SYMBOL
03F1 ϱ GREEK RHO SYMBOL
Subscribe to:
Posts (Atom)
my-alpine and docker-compose.yml
``` version: '1' services: man: build: . image: my-alpine:latest ``` Dockerfile: ``` FROM alpine:latest ENV PYTH...
-
It took me a while to figure out how to insert a space in Mathtype equations. This is especially useful when you write an equation with mult...
-
Recently I read post from Dr. Doug Hull's blog: http://blogs.mathworks.com/videos/2009/10/23/basics-volume-visualization-19-defining-s...
-
A couple of days ago, I plotted about twenty figures and was trying to set the markers with thicker edge. This would be very easy if it was ...