Tuesday, September 2, 2008

Using system command in different softwares

Matlab: ! COMMAND
Grads: ! COMMAND
Fortran: call system(COMMAND, status)
C: system("COMMAND")
VB: shell "COMMAND"
Ferret: sp system command
Python: import os or os.system('COMMAND')
PHP: system('command',$returnvalue) or exec('command',$output, $returnvalue)
R: system('command')
Continue.....

Monday, September 1, 2008

[Matlab] Reading Ascii data with matlab

function DataFromFile=ReadAsciiData(FileName,FMT,NumFmt)
% Function to read ascii data from a file with given format
% Usage:
%            DataFromFile=GetAsciiData(FileName,FMT,NumFmt)
%       e.g.,
%            Data=GetAsciiData('data.dat','%8f',4)
%            The data will be read in following command:
%                   Data=fscanf(fid,'%8f %8f %8f %8f',[4 inf]);
%            Same as: Data=ReadAsciiData('data.dat','%8f %8f %8f %8f')
%            http://scriptdemo.blogspot.com

fid=fopen(FileName,'r');
if (fid==-1)
   error(['Error in reading ',FileName,', Please check the file name.']);
else
   if ~exist('FMT');
      FMT='%f';
      if ~exist('NumFmt') NumFmt=1; end
      for N=1:NumFmt-1
           FMT=[FMT,' ','%f'];
      end
   else
      if ~exist('NumFmt')
         NumFmt=length(find(FMT=='%'));
      else
         for N=1:NumFmt-1
               FMT=[FMT,' ',FMT];
         end
      end
   end
   DataFromFile=fscanf(fid,FMT,[NumFmt,inf]); % [DataFromFile]=fscanf(fid,FMT);
   DataFromFile=DataFromFile';
   fclose(fid);
end

[Matlab] Saving 2D data in ASCII with matlab

function SaveDataAscii(data,filename,FileHead,Gformat)
% to save a 2d array into a ascii file as what you see in array editor
%     a format option is also provided
% Usage:
%     SaveDataAscii(data,filename,FileHead,Gformat)
% e.g.,
%       my2ddata=rand(3,5);
%       SaveDataAscii(my2ddata,'myrand2dtest.dat','it is a test','%7.4f');


isHeader=1;
if nargin==3
   Gformat='%10.4f';
elseif nargin==2
   isHeader=0;
   Gformat='%10.4f';
elseif nargin~=4
   help SaveDataAscii
   return
end
if (length(FileHead)==0)
   isHeader=0;
end

% open the file
fid=fopen(filename,'w');
if isHeader==1
   fprintf(fid,['%% ',FileHead]);
   fprintf(fid,'\n');
end

% write the datablock
for i=1:size(data,1)
    for j=1:size(data,2)
        fprintf(fid,Gformat,data(i,j));
    end;
    fprintf(fid,'\n');
end;
fclose(fid);

[Matlab] Plot rectangles with matlab

The script is not really useful. However, it shows how a matlab function works. More matlab functions will be added later.
function PlotRect(Xs,Ys,Xlen,Ylen,LineC,LineS,LineM)
% To draw rectangles with given position and properties
% Usage: PlotRect(Xs,Ys,Xlen,Ylen,LineC,LineS,LineM)
%  e.g.,
%        PlotRect(0.1,0.13,0.7,0.6,'k','-','.')
%        http://scriptdemo.blogspot.com

if exist('LineC','var')~=1
   LineC='k';
end
if exist('LineS','var')~=1
   LineS='-';
end
if exist('LineM','var')~=1
   LineM='none';
end
if ~ishold on; hold on; end

xx=[Xs Xs+Xlen Xs+Xlen Xs Xs];
yy=[Ys Ys Ys+Ylen Ys+Ylen Ys];
hp=plot(xx,yy); set(hp,'color',LineC,'linestyle',LineS,'Marker',LineM);

ShowCalendar