MATLAB數據保存成其他格式文件(.csv .xlsx .txt等)通用函數【faruto出品】
FQuantToolBox發布後有些網友反應能否將獲取的股票等相關數據保存成其他文件格式,當下FQuantToolBox默認的保存格式是.mat文件,可能有的朋友需要存成其他格式(.csv .xlsx .txt等)的文件進行調用,故編寫了SaveData2File.m函數,一方面可以結合FQuantToolBox使用,將提取的股票、期貨數據保存成您需要的格式,另一方面該函數也可以單獨使用,可以將任何MATLAB數據(double型 cell型)快速的保存成其他格式文件(現支持保存成{.txt,.dat,.csv,.xls,.xlsb,.xlsx,.xlsm}等擴展名的文件)。
查看FQuantStudio微信公眾號歷史消息或進入公眾號後 輸入"FQuantToolBox",可以查看FQuantToolBox相關文件。SaveData2File.m源碼
function [Status, Message] = SaveData2File(Data, FileName, ColNamesCell)n%% SaveData2Filen% by LiYang_faruton% Email:farutoliyang@foxmail.comn% 2015/05/01n%% 輸入輸出預處理nStatus = 1;nMessage = [];nif nargin < 3 || isempty(ColNamesCell)n ColNamesCell = [];nendnif nargin < 2 || isempty(FileName)n FileName = OutData.xlsx;nendnif nargin < 1 || isempty(Data)n Status = 0;n Message = 缺少輸入參數,請輸入待保存的數據!;n disp(Message);n return;nendnn% ColNamesCell 處理n[Rlen, Clen] = size(Data);nif ~isempty(ColNamesCell)n tlen = length(ColNamesCell);n if tlen < Clenn for i = tlen+1:Clenn str = [VarName,num2str(i)];n ColNamesCell{i} = str;n endn endn if tlen > Clenn ColNamesCell = ColNamesCell(1:Clen);n endnendnn% FileName 檢查處理nind = find(FileName == ., 1,last);nif isempty(ind)n FileName = [FileName,.xlsx];n ind = find(FileName == ., 1,last);nendnExtCell = {.txt,.dat,.csv,.xls,.xlsb,.xlsx,.xlsm};nnExtName = FileName(ind:end);nif ~ismember(ExtName, ExtCell)n Status = 0;n Message = 請檢查輸入的文件擴展名!(僅支持如下擴展名);n disp(Message);n disp(ExtCell);n return; nendn%% Mainnnswitch class( Data )n case doublen tCell = num2cell(Data);n if ~isempty( ColNamesCell )n tCell = [ColNamesCell;tCell];n endn Fun = @(x)( num2str(x) );n tCell = cellfun( Fun,tCell, UniformOutput, false);n n T = cell2table(tCell);n writetable(T,FileName,WriteVariableNames,false);n n case celln tCell = Data;n if ~isempty( ColNamesCell )n tCell = [ColNamesCell;tCell];n endn Fun = @(x)( num2str(x) );n tCell = cellfun( Fun,tCell, UniformOutput, false);n n T = cell2table(tCell);n writetable(T,FileName,WriteVariableNames,false); n n case structn n n n otherwisen Status = 0;n Message = 輸入數據類型未知!請檢查!;n disp(Message);n return;nendnn%% sub funn
測試腳本(測試數據來源,需要配合FQuantToolBox)
%% 獲取股票代碼列表測試nn[StockList,StockListFull] = GetStockList_Web;nStockCodeDouble = cell2mat( StockList(:,3) );nsave(StockList,StockList);nn%% StockList SaveData2Filentic;nData = StockList;nnFileName = StockList.csv;nColNamesCell = {股票名稱,股票代碼,股票代碼(純數字)};nn[Status, Message] = SaveData2File(Data, FileName, ColNamesCell);nnData = StockList;nnFileName = StockList.txt;nColNamesCell = {股票名稱,股票代碼,股票代碼(純數字)};nn[Status, Message] = SaveData2File(Data, FileName, ColNamesCell);ntoc;n%% 獲取指數代碼列表nn[IndexList] = GetIndexList_Web;nnsave(IndexList,IndexList);nn%% IndexList SaveData2Filentic;nData = IndexList;nnFileName = IndexList.csv;nColNamesCell = {名稱,代碼,代碼(純數字)};nn[Status, Message] = SaveData2File(Data, FileName, ColNamesCell);ntoc;n%% 獲取股票日線(除權除息)數據測試nnStockCode = sh600030;nnBeginDate = 20130101;nEndDate = 20150101;nn[StockDataDouble,adjfactor] = GetStockTSDay_Web(StockCode,BeginDate,EndDate);nn%% StockDataDouble SaveData2Filentic;nData = StockDataDouble;nFileName = StockDataTest.csv;n% FileName = [];nnColNamesCell = {日期,開,高,低,收,量,額,復權因子};n% ColNamesCell = [];nn[Status, Message] = SaveData2File(Data, FileName, ColNamesCell);ntoc;nn%% 獲取股票財務指標測試nnStockCodeInput = 600588;nStockCodeInput = StockCode_G;nnYear = 2014;n[FIndCell,YearList] = GetStockFinIndicators_Web(StockCodeInput,Year);nFIndCelln%% FIndCell SaveData2Filentic;nData = FIndCell;nnFileName = 財務指標Test.csv;nColNamesCell = [];nn[Status, Message] = SaveData2File(Data, FileName, ColNamesCell);ntoc;n
運行結果
保存後的其他格式的文件推薦閱讀:
※投資績效分析指標系列之一:MaxDrawDown&MaxDrawDownDuration
※大數據指數對比分析
※股票策略回測的框架、實現、測試——以動量策略為例
TAG:FQuantStudio |