function mystrcel=num2strcell(mynum,GFmat) % to convert a numeric array into a string cell % usage: % mystrcel=num2strcell(YouNumArray,Digit.Format); % e.g., mystrcel=num2strcell(0.1:0.1:0.5,'%3.2f'); % copyright by http://scriptdemo.blogspot.com [nrow,ncol]=size(mynum); for nj=1:nrow for ni=1:ncol mystrcel{nj,ni}=num2str(mynum(nj,ni),GFmat); end end |
Wednesday, December 22, 2010
num2strcell
read netcdf file using native matlab functions
UPDATED VERSION: Here function ncvardata=GetNcVarMatlab(ncfile,ncvar,RangeStr) % to read data from the netcdf file using native matlab netcdf-functions % Usage: vardata=GetNcVarMatlab(nc-filename,nc-varname,Dimension-Range) % e.g., % mydata=GetNcVarMatlab('mask.nc','tmask','1:1,1:46,1:400,1:568'); % RangeStr here is similar to the output of ncdump % copyright by http://scriptdemo.blogspot.com IsDebug=0; if ~exist(ncfile,'file') error([ncfile,' can not been found']); return end ncfid=netcdf.open(ncfile,'NC_NOWRITE'); % get dimension length [numdims, numvars, numglobalatts, unlimdimID] = netcdf.inq(ncfid); for NDim=1:numdims [dimname{NDim}, dimlen(NDim)] = netcdf.inqDim(ncfid,NDim-1); end %var infor varid=netcdf.inqVarID(ncfid,ncvar); [varname,varxtype,vardimids,varnumatts]=netcdf.inqVar(ncfid,varid); numdims=length(vardimids); % some variables may have less dimensions if ~exist('RangeStr','var') RangeStr=':'; for nd=2:numdims RangeStr=[RangeStr,',:']; end end IndC=strfind(RangeStr,',');% index of commas if length(IndC)~=numdims-1 disp(['Input dimension range is not correct, should be: ',num2str(numdims),'-d']); return end startstr=''; countstr=''; stridestr=''; for nn=1:length(IndC)+1 if nn==1 tmpstr=RangeStr(1:IndC(1)-1); elseif nn<numdims tmpstr=RangeStr(IndC(nn-1)+1:IndC(nn)-1); else tmpstr=RangeStr(IndC(nn-1)+1:end); end [tmpstart,tmpcount,tmpstride]=GetIndFromStr(tmpstr,':',dimlen(vardimids(numdims-nn+1)+1)); if nn==1 startstr=[num2str(tmpstart),'],']; countstr=[num2str(tmpcount),'],']; stridestr=[num2str(tmpstride),']']; elseif nn==numdims startstr=[',[',num2str(tmpstart),',',startstr]; countstr=['[',num2str(tmpcount),',',countstr]; stridestr=['[',num2str(tmpstride),',',stridestr]; else startstr=[num2str(tmpstart),',',startstr]; countstr=[num2str(tmpcount),',',countstr]; stridestr=[num2str(tmpstride),',',stridestr]; end end if IsDebug disp(['ncvardata=netcdf.getVar(ncfid,varid',startstr,countstr,stridestr,');']); end eval(['ncvardata=netcdf.getVar(ncfid,varid',startstr,countstr,stridestr,');']); netcdf.close(ncfid); ncvardata=permute(ncvardata,length(size(ncvardata)):-1:1); function [numstart,numcount,numstride]=GetIndFromStr(tmpstr,ctag,Ndim) % Read the numbers from the special string IsDebug=0; IndC=strfind(tmpstr,ctag); if isempty(IndC) numstart=str2double(tmpstr)-1; numcount=1; numstride=1; else switch length(IndC) case {1} switch IndC case {1} % : and :num if strcmp(tmpstr,':') numstart=0; numcount=Ndim; numstride=1; else if strcmp(tmpstr(2:end),'end') numstart=0; numcount=Ndim; numstride=1; else numstart=0; numcount=str2double(tmpstr(2:end))-1; numstride=1; end end case {length(tmpstr)} if strcmp(tmpstr,':') numstart=0; numcount=Ndim; numstride=1; return else numstart=str2double(tmpstr(1:IndC-1))-1; numcount=Ndim-numstart; numstride=1; end otherwise numstart=str2double(tmpstr(1:IndC-1))-1; if strcmp(tmpstr(IndC+1:end),'end') numend=Ndim; else numend=str2double(tmpstr(IndC+1:end)); end numcount=numend-numstart; numstride=1; end case {2} % num0:stride:num1 if IndC(1)==1 numstart=0; else numstart=str2double(tmpstr(1:IndC(1)-1))-1; end if IndC(2)==IndC(1)+1 numstride=1; else numstride=str2double(tmpstr(IndC(1)+1:IndC(2)-1)); end if IndC(2)==length(tmpstr) numend=Ndim; else if strcmp(tmpstr(IndC(2)+1:end),'end') numend=Ndim; else numend=str2double(tmpstr(IndC(2)+1:end)); end end numcount=numend-numstart; otherwise error('Not defined range type') end end if IsDebug disp(['tmpstr=',tmpstr]) disp(['start:',num2str(numstart), ' stride:',num2str(numstride),' count:',num2str(numcount)]) end |
Friday, December 17, 2010
Visualize Julia Sets using Matlab
function myjulia(Zmax,c,N) % Generate and visualize quadratic Julia Sets % More information about Julia Sets can be found here: % http://en.wikipedia.org/wiki/Julia_set % this code is for the assignment of the "Introduction to Matlab" offered by MITOPENCOURSEWARE % Coded by http://scriptdemo.blogspot.com if (nargin==1) Ndemo=Zmax; clear Zmax switch Ndemo case {1} myjulia(1,-0.297491+i*0.641051,100); return; case {2} myjulia(0.35,-0.297491+i*0.641051,250); return; otherwise disp('Not defined demo type!') help myjulia; return end elseif (nargin~=3) help myjulia; return end % generate the basic matrix NM=500; [Z,tmpy]=meshgrid(linspace(-Zmax,Zmax,NM),zeros(1,NM)); Z=Z+i*Z'; clear tmpy % compute the escape velocity myM=reshape(escapeVelocity(Z(:),c,N),NM,NM); % visualize the results imagesc(atan(0.1*myM)); figurenicer;axis xy; function n=escapeVelocity(z0,c,N) n=z0*0; NLen=length(z0); IndZ=1:length(z0); IndZ=IndZ'; for ni=1:N IndLT=find(abs(z0)<2); IndGE=find(abs(z0)>=2); n(IndZ(IndGE))=ni; if (length(IndLT)>0) z0(IndLT)=z0(IndLT).*z0(IndLT)+c; end z0(IndGE)=[]; IndZ(IndGE)=[]; end if ~isempty(IndZ) n(IndZ)=N; end |
myjulia(2) |
Labels:
figure,
Julia Sets,
Matlab,
script,
visualization
Generate a series of column names in R
#if you want a series of column names, e.g., col1,col2,col3, ..., #you can use: mydf<-data.frame(matrix(ncol=5,nrow=4)) colnames(mydf)<-c("col1","col2","col3","col4","col5") #Well, it does work, but how about for 50 columns? #A loop! Sounds great. Okay, mydf<-data.frame(matrix(ncol=50,nrow=4)) for (n in 1:ncol(mydf)) { colnames(mydf)[n]<-paste("col",n,sep="") } #But again, it's as urgly as the textbook. Here we offer a much better solution: colnames(mydf)<-paste(rep("col",ncol(mydf)),c(1:ncol(mydf)),sep="") #Smart, eh? At least the nicer looking :) #From http://scriptdemo.blogspot.com |
Trim a string in R
# sub-functions # Copyright: http://scriptdemo.blogspot.com mytrim<-function(oristr) { oristr<-sub("^ +","",oristr) oristr<-sub(" +$","",oristr) # or gsub("(^ +)|( +$)","",oristr) return(oristr) } |
Subscribe to:
Posts (Atom)