?? fxdownload.m
字號:
function fxdownload(fileid,isselectPath,indexPath)
%FXDOWNLOAD Download a file from MATLAB Central File Exchange.
%
% FXDOWNLOAD(fileid) downloads a file with specified fileid into the
% indexPath\fxdownloads directory. The indexPath is the downloads
% location, retrieved from previous download. If downloading for the
% first time it will prompt the user to select the download directory. If
% the downloading file is a zip file, it will extracts the archived
% contents of the file or else for all other formats just downloads the
% file. These files are downloaded into a directory with name format
% [filename][fileid] and after the download is complete it will CD to
% this directory and opens up fxsummary.html page.
%
% FXDOWNLOAD(fileid,isselectPath) downloads the file with specified
% fileid with an option of selecting a indexPath, if isselectPath
% is 1. If isselectPath is 0 indexPath is choosen from previous download.
%
% FXDOWNLOAD(fileid,isselectPath,indexPath) downloads the file with
% specified fileid with an option of passing indexPath. When passing an
% indexPath isselectPath should be 0. If isselectPath is 1 it will
% prompt the user to choose indexPath and overwrites the value that is
% passed.
%
% Example:
% fxdownload(1613)
% fxdownload(1613,1)
% fxdownload(1613,0,'C:\matlab')
%
% NOTE: 1.Fileid can be found in MATLAB Central->File Exchange site.
% 2.For every new indexPath, this function will
% create a fxdownloads directory with in indexPath and then
% download the file into respective file name. If for the
% next download another directory, one level above or below the
% fxdownloads, is choosen then it will not create fxdownloads
% directory again. It will download the file into previous
% fxdownloads directory.
% Copyright 1984-2006 The MathWorks, Inc.
% Author : Santosh Kasula
%default is set to 0 to get the value from getpref
if (nargin < 2), isselectPath = 0; end
if (nargin < 3), indexPath = ''; end
mainIndexPath = fullfile(fileparts(prefdir),'fxdownloads');
[indexPath,rootPath] = selectIndexPath(isselectPath,indexPath,mainIndexPath);
%exit if no indexPath is selected
if rootPath == 0
return
end
intro = '<h1><center>File Exchange Downloads</center></h1>';
indexHtmlFilename = 'downloads_index.html';
searchstr = ['http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=' sprintf('%.0f',fileid)];
htmlText = urlread(searchstr);
filename = strrep(regexp(htmlText,'"fileName" value="(.*?)">','tokens','once'),' ','%20');
%File Extention
fe = regexp(htmlText,'name="fileExt" value="(.*?)">','tokens','once');
%contributorId
contributorId = regexp(htmlText,'"contributorId" value="(.*?)">','tokens','once');
%Download file URL
downloadFileURL = ['http://www.mathworks.com/matlabcentral/fileexchange/download.do?objectId=' sprintf('%.0f',fileid) ...
'&fn=' filename{1} '&fe=' fe{1} '&cid=' contributorId{1}];
%Create a donwloaDir
[downloadDir,dirname,isOverWrite] = createdownloadDir(indexPath,fileid,filename);
if strcmp(isOverWrite,'Cancel')
return
end
%download file
unzipfilenames = downloadfile(downloadFileURL,downloadDir,filename,fe);
%Get the relevant html info in a structure
d = relevantHtmlInfo(htmlText,fileid,filename,dirname,downloadDir,contributorId,unzipfilenames);
%Create an index page for all downloads in a particular location
writeIndex(indexPath,indexHtmlFilename,downloadDir,intro,d);
%Create a summary page for each individual download
createSummaryPage(indexPath,indexHtmlFilename,downloadDir)
%Create main index for all downloads in all locations
[dirPath fxdirname] = fileparts(fileparts(indexPath));
if strcmp(fxdirname,'fxdownloads')
updatefxindex(fullfile(fileparts(indexPath),indexHtmlFilename),0);
end
%open fxsummary page
web(fullfile(downloadDir,'fxsummary.html'));
%==========================================================================
function [indexPath,rootPath] = selectIndexPath(isselectPath,indexPath,mainIndexPath)
%SELECTINDEXPATH Select a direcoty to download a file.
%get the indexPath from the preference already set if indexPath is not
%passed
if ispref('fileexchange','prefindexPath') && isempty(indexPath)
indexPath = getpref('fileexchange','prefindexPath');
end
rootPath = '';
%get the user input for indexPath if it is empty or user want to change the
%indexPath preference.
if isempty(indexPath) || isselectPath
rootPath = uigetdir(cd,'Select a folder');
%
if rootPath == 0
%exit if no directory is choosen
return;
end
cd(rootPath);
[dirpath filename ext] = fileparts(rootPath);
%check the validity of the directory
if isempty(ext)
%check if the directory choosen is fxdownloads. if fxdownloads direcoty
%is choosen assign user selected directory as indexPath or else
%create a fxdownloads directory and assign it indexPath
if ~isempty(filename)
[localdirPath localFilename] = fileparts(dirpath);
if ~strcmp(filename,'fxdownloads') && ~strcmp(localFilename,'fxdownloads')
[subdirpath subfilename] = fileparts(dirpath);
if strcmp(subfilename,'fxdownloads')
indexPath = rootPath;
else
if ~isdir('fxdownloads')
[status, message, mesageid] = mkdir('fxdownloads');
if status == 0
errordlg([mesageid ': ' message],'Error');
end
indexPath = fullfile(rootPath,'fxdownloads');
else
indexPath = fullfile(rootPath,'fxdownloads');
end
end
else
indexPath = rootPath;
end
else
indexPath = fullfile(rootPath,'fxdownloads');
end
if ispref('fileexchange','prefindexPath')
setpref('fileexchange','prefindexPath',indexPath);
else
addpref('fileexchange','prefindexPath',indexPath);
end
else
errordlg('Invalid filname.', 'Error');
end
end
%check if prefindexPath preferece is already existing if not add prefindexPath
%preference to fileexchange group
saveindexPath(indexPath,mainIndexPath);
%==========================================================================
function saveindexPath(indexPath,mainIndexPath)
%SAVEINDEXPATH Save the selected indexPath into a mat file.
if ~isdir(mainIndexPath)
mkdir(mainIndexPath)
end
filename = dir(fullfile(mainIndexPath,'indexPathList.mat'));
if ~isempty(filename)
load(fullfile(mainIndexPath,'indexPathList.mat'),'newindexPathList');
for i=1:length(newindexPathList)
if strcmp(newindexPathList(i).indexPath,indexPath)
isindexPathexisting = true;
newindexPathList(i).indexPath = indexPath;
break;
else
isindexPathexisting = false;
end
end
if ~isindexPathexisting
newindexPathList(length(newindexPathList)+1).indexPath = indexPath; %#ok<NASGU>
end
else
newindexPathList(1).indexPath = indexPath;
end
save (fullfile(mainIndexPath,'indexPathList.mat'),'newindexPathList');
%==========================================================================
function [downloadDir,dirname,isOverWrite] = createdownloadDir(indexPath,fileid,filename)
%CREATEDOWNLOADDIR Create a directory name in the format [filename][fid].
dirname = strrep(strcat(strrep(filename{1},'.','_'),sprintf('%.0f',fileid)),'%20','_');
downloadDir = strrep(fullfile(indexPath,dirname),'%20',' ');
isdirExsting = isdir(downloadDir);
%by default do not overwrite a folder
isOverWrite = 'yes';
%If directroy already existing ask the user to Overwrite/Cancel?
if isdirExsting
isOverWrite = questdlg('The Directory already exists. Do you want to Overwrite? ','Directory already existing','Ok','Cancel','Ok');
% ButtonName = questdlg('What is your favorite color?','Color Question','Red', 'Green', 'Blue', 'Green');
if isempty(isOverWrite)
isOverWrite = 'Cancel';
end
switch lower(isOverWrite)
case 'ok'
cd ..
rmdir(downloadDir,'s')
case 'cancel'
return
end
end
%Create a Download directory
[status, message, mesageid] = mkdir(downloadDir);
%Return if there is any error in the directory creation
if status == 0
error(mesageid, message);
end
%==========================================================================
function unzipfilenames = downloadfile(downloadFileURL,downloadDir,filename,fe)
%DOWNLOADFILE Download a file.
%Download the file
if strcmp(fe{1},'.zip')
%If zip file, unzip the file in download directory
tempdownloadDir = fullfile(tempdir,'fxdownloads');
mkdir(tempdownloadDir);
[tempHtmlfile,status] = urlwrite(downloadFileURL,fullfile(tempdownloadDir,strcat(filename{1},'.html')));
if isequal(status,1)
unzip(tempHtmlfile, tempdownloadDir);
delete(tempHtmlfile);
else
error('Error unzipping the URL')
end
dirlist = getsubdirs(tempdownloadDir);
mfilecount=0;
allfilecount = 0;
for n=1:length(dirlist)
allfiles = dir(dirlist{n});
allfiles = allfiles(logical(~[allfiles.isdir]));
if ~isempty(allfiles)
for k=1:length(allfiles)
allfilecount = allfilecount + 1;
allunzipfiles{allfilecount} = fullfile(dirlist{n},allfiles(k).name);
end
end
end
dirfiles = tempdownloadDir;
while true
tempdirfiles = dir(dirfiles);
if length(tempdirfiles) <= 3 && length(tempdirfiles(logical([tempdirfiles.isdir]))) >= 3
for n = 1:length(tempdirfiles)
if ~strcmp(tempdirfiles(n).name(1),'.') && ...
~strcmp(tempdirfiles(n).name,'.') && ...
~strcmp(tempdirfiles(n).name,'..') && ...
tempdirfiles(n).isdir
tempdirfilename = tempdirfiles(n).name;
end
end
dirfiles = fullfile(dirfiles,tempdirfilename);
else
break;
end
end
[status,message,messageid] = copyfile(dirfiles,downloadDir,'f');
for i=1:length(allunzipfiles)
unzipfilenames(i) = strrep(strrep(allunzipfiles(i),dirfiles,downloadDir),'/','\');
end
%Return if there is any error in the directory moving
if status == 0
error(mesageid, message);
end
cd(tempdir);
rmdir(tempdownloadDir,'s');
else
%any file extension save the file in download directory
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -