Showing posts with label Utility Script. Show all posts
Showing posts with label Utility Script. Show all posts

Sunday, February 14, 2010

split_str_by_comma.m


function ret_cell_array=split_str_by_comma(str)
%
% This function is to split a str by comma, return as cell array
%
% EXAMPLE:
% ret_cell_array=split_str_by_comma('12, 34, 56');
% the ret_cell_array should be {'12','34','56'}
%
% If you have MATLAB, you can use ret_cell_array=regexp(str,',','split') to get job done;
%
%   Copyright 2010 EdgeMe
%   EdgeMe, 10-Feb-2010
%
%

    %find out where are the commas
    commas=regexp(str,',');

    %if no comma, just return the str
    if isempty(commas)
        ret_cell_array={str};
        return;
    else %we found comma(s)

        %first comma, also get rid of space
        ret_cell_array{1}=strrep(str(1:commas(1)-1),' ','');

        for i=1:numel(commas)-1

            ret_cell_array{end+1}=strrep(str(commas(i)+1:commas(i+1)-1),' ','');

        end

        %last one
        if length(str)>commas(end)
            ret_cell_array{end+1}=strrep(str(commas(end)+1:length(str)),' ','');
        end


    end


 end

Saturday, February 13, 2010

save2html.m


function save2html(htmlfile,cells,title)
%
%   This script is to save cells into a table format html file
%
%   Copyright 2010 EdgeMe
%   EdgeMe, 10-Feb-2010
%
%

fid = fopen(htmlfile,'w');
fprintf(fid,['<table style="color:black; font-size:20; font-family:Times;" cellpadding="0" cellspacing="0" border="1">']);

fprintf(fid,['<title>',title,'</title>']);

[r c] = size(cells);

if r==1 && c>1 % transpose the cells
    cells=cells';
end

[r c] = size(cells);

for j=1:r; % for each row

 fprintf(fid,'<tr>');

 for i=1:c; %for each column

  temp = cells{j,i};
        if ~isempty(temp)
       if isnumeric(temp) && ~isnan(temp);
       td = num2str(temp);
        elseif ischar(temp)
       td = temp;
             end

        else
            td = '&nbsp;';
        end

        fprintf(fid,['<td>','<a href="http://www.finviz.com/quote.ashx?t=',td,'" target="_blank">',td,'</td>']);

 end

 fprintf(fid,'</tr>');

end


fprintf(fid,'</table>');
fclose(fid);

end

get_symbols.m


function symbols=get_symbols(symbolfile)
%
% This function return all symbols from a symbol file
% A symbol file is a simple text file contain a stock symbol at each line
% EXAMPLE:
% symbols=get_symbols('all_symbols.txt');
%
% If you have MATLAB, you can just use "importdata(symbolfile)" to get job done.
% If you run this script in MATLAB, please change fgetline to fgetl
%
% 12-Feb-2010, added deblank to make sure no space
%
%   Copyright 2010 EdgeMe
%   EdgeMe, 10-Feb-2010
%
%
    symbols=[];
    symbolpath='c:\relativestrength\list';

    datafile=[symbolpath,'\',symbolfile];

     if exist(datafile)>0

         fid = fopen(datafile);
         while (~feof(fid))
             temp=fgetline(fid); % if run in the MATLAB, use fgetl(fid)
             newlinestart= regexp(temp,'\n') ;%get the line, but the return may contains a empty new line

             if isempty(newlinestart) %means no empty new line
                    symbols{end+1} =deblank(temp);
             else

                    symbols{end+1} =deblank(temp(1:newlinestart-1)); % only save the content without the empty new line
             end
         end
     else
        warning(['Could not find the file ',symbolfile]);
         return;
     end

    end