FQuantToolBox發(fā)布后有些網(wǎng)友反應(yīng)能否將獲取的股票等相關(guān)數(shù)據(jù)保存成其他文件格式,,當(dāng)下FQuantToolBox默認(rèn)的保存格式是.mat文件,可能有的朋友需要存成其他格式(.csv .xlsx .txt等)的文件進(jìn)行調(diào)用,,故編寫了SaveData2File.m函數(shù),,一方面可以結(jié)合FQuantToolBox使用,將提取的股票,、期貨數(shù)據(jù)保存成您需要的格式,,另一方面該函數(shù)也可以單獨(dú)使用,可以將任何MATLAB數(shù)據(jù)(double型 cell型)快速的保存成其他格式文件(現(xiàn)支持保存成{'.txt','.dat','.csv','.xls','.xlsb','.xlsx','.xlsm'}等擴(kuò)展名的文件),。
查看FQuantStudio微信公眾號(hào)歷史消息或進(jìn)入公眾號(hào)后 輸入'FQuantToolBox',,可以查看FQuantToolBox相關(guān)文件。
SaveData2File.m源碼
function [Status, Message] = SaveData2File(Data, FileName, ColNamesCell)
%% SaveData2File
% by LiYang_faruto
% Email:[email protected]
% 2015/05/01
%% 輸入輸出預(yù)處理
Status = 1;
Message = [];
if nargin < 3 || isempty(ColNamesCell)
ColNamesCell = [];
end
if nargin < 2 || isempty(FileName)
FileName = 'OutData.xlsx';
end
if nargin < 1 || isempty(Data)
Status = 0;
Message = '缺少輸入?yún)?shù),,請輸入待保存的數(shù)據(jù),!';
disp(Message);
return;
end
% ColNamesCell 處理
[Rlen, Clen] = size(Data);
if ~isempty(ColNamesCell)
tlen = length(ColNamesCell);
if tlen < Clen
for i = tlen+1:Clen
str = ['VarName',num2str(i)];
ColNamesCell{i} = str;
end
end
if tlen > Clen
ColNamesCell = ColNamesCell(1:Clen);
end
end
% FileName 檢查處理
ind = find(FileName == '.', 1,'last');
if isempty(ind)
FileName = [FileName,'.xlsx'];
ind = find(FileName == '.', 1,'last');
end
ExtCell = {'.txt','.dat','.csv','.xls','.xlsb','.xlsx','.xlsm'};
ExtName = FileName(ind:end);
if ~ismember(ExtName, ExtCell)
Status = 0;
Message = '請檢查輸入的文件擴(kuò)展名!(僅支持如下擴(kuò)展名)';
disp(Message);
disp(ExtCell);
return;
end
%% Main
switch class( Data )
case 'double'
tCell = num2cell(Data);
if ~isempty( ColNamesCell )
tCell = [ColNamesCell;tCell];
end
Fun = @(x)( num2str(x) );
tCell = cellfun( Fun,tCell, 'UniformOutput', false);
T = cell2table(tCell);
writetable(T,FileName,'WriteVariableNames',false);
case 'cell'
tCell = Data;
if ~isempty( ColNamesCell )
tCell = [ColNamesCell;tCell];
end
Fun = @(x)( num2str(x) );
tCell = cellfun( Fun,tCell, 'UniformOutput', false);
T = cell2table(tCell);
writetable(T,FileName,'WriteVariableNames',false);
case 'struct'
otherwise
Status = 0;
Message = '輸入數(shù)據(jù)類型未知,!請檢查,!';
disp(Message);
return;
end
%% sub fun
測試腳本(測試數(shù)據(jù)來源,需要配合FQuantToolBox)
%% 獲取股票代碼列表測試
[StockList,StockListFull] = GetStockList_Web;
StockCodeDouble = cell2mat( StockList(:,3) );
save('StockList','StockList');
%% StockList SaveData2File
tic;
Data = StockList;
FileName = 'StockList.csv';
ColNamesCell = {'股票名稱','股票代碼','股票代碼(純數(shù)字)'};
[Status, Message] = SaveData2File(Data, FileName, ColNamesCell);
Data = StockList;
FileName = 'StockList.txt';
ColNamesCell = {'股票名稱','股票代碼','股票代碼(純數(shù)字)'};
[Status, Message] = SaveData2File(Data, FileName, ColNamesCell);
toc;
%% 獲取指數(shù)代碼列表
[IndexList] = GetIndexList_Web;
save('IndexList','IndexList');
%% IndexList SaveData2File
tic;
Data = IndexList;
FileName = 'IndexList.csv';
ColNamesCell = {'名稱','代碼','代碼(純數(shù)字)'};
[Status, Message] = SaveData2File(Data, FileName, ColNamesCell);
toc;
%% 獲取股票日線(除權(quán)除息)數(shù)據(jù)測試
StockCode = 'sh600030';
BeginDate = '20130101';
EndDate = '20150101';
[StockDataDouble,adjfactor] = GetStockTSDay_Web(StockCode,BeginDate,EndDate);
%% StockDataDouble SaveData2File
tic;
Data = StockDataDouble;
FileName = 'StockDataTest.csv';
% FileName = [];
ColNamesCell = {'日期','開','高','低','收','量','額','復(fù)權(quán)因子'};
% ColNamesCell = [];
[Status, Message] = SaveData2File(Data, FileName, ColNamesCell);
toc;
%% 獲取股票財(cái)務(wù)指標(biāo)測試
StockCodeInput = '600588';
StockCodeInput = StockCode_G;
Year = '2014';
[FIndCell,YearList] = GetStockFinIndicators_Web(StockCodeInput,Year);
FIndCell
%% FIndCell SaveData2File
tic;
Data = FIndCell;
FileName = '財(cái)務(wù)指標(biāo)Test.csv';
ColNamesCell = [];
[Status, Message] = SaveData2File(Data, FileName, ColNamesCell);
toc;
運(yùn)行結(jié)果 保存后的其他格式的文件
|