2012-02-23 21 views
1

我已經設法讓matlab登錄到Google趨勢來下載csv數據。首先,我使用了DownloadString,然後在使用fastawrite保存csv文件之前將其轉換爲matlab字符串。調用WebClient.DownloadFile時,Matlab登錄和下載失敗

但是,即使字符串被正確下載,'\ n'行正在失去一些格式化...這是非常奇怪的,因爲如果我通過'\ n'將文件分割成單元格數組,格式化是好的!

所以我現在試圖讓DownloadFile方法來工作,但我不斷收到以下錯誤:

無方法「DownloadFile」與發現類「System.Net.WebClient」

匹配簽名

千恩萬謝,

這裏的功能:

NET.addAssembly('System.Net'); 

url = 'https://www.google.com/accounts/ClientLogin?accountType=GOOGLE&Email=email&Passwd=pass&service=trendspro&source=test-test-v1'; 
durl = System.String(strcat('http://www.google.com/trends/viz?q=', keyWord, '&date=all&geo=all&graph=all_csv&sort=0&scale=1&sa=N')); 

if exist('googleWebClient','var') 
    client = googleWebClient; 
else 
    client = System.Net.WebClient; 

    response = client.DownloadString(url); 
    sid = char(response.ToString); 
    sid = regexp(sid, '\n', 'split'); sid = sid(1,1); 

    client.Headers.Add('Cookie', char(sid)); 
    assignin('base','googleWebClient',client); 
end 

saveFilePath = System.String(strcat('C:\Dropbox\PROJECTS\', keyWord, '.csv')); 


data = client.DownloadFile(durl, saveFilePath); 

回答

0

什麼樣的格式正在消失?聽起來像你可能只是獲得Unix模式(\ n)文本,然後在DOS模式(\ r \ n)編輯器中打開它。快速regexprep(sid, '\r?\n', sprintf('\r\n'))將解決這個問題。切換到DownloadFile可能不會。

就你所得到的「沒有這種方法」的錯誤而言,WebClient.DownloadFile有一個void返回類型。看起來像Matlab的.NET支持認爲簽名的一部分。刪除「數據=」分配,它會消失。

>> client = System.Net.WebClient; 
>> x = client.DownloadFile('http://www.cnn.com', 'C:\temp\blah.html') 
No method 'DownloadFile' with matching signature found for class 'System.Net.WebClient'. 

>> client.DownloadFile('http://www.cnn.com', 'C:\temp\blah.html') 
>> 
+0

非常感謝!我一直在禁止我的頭靠在牆上....我刪除了data =,它正在下載csv文件。看來,當我使用char(數據)函數從System.String轉換爲matlab char,然後使用fastawrite進行保存時,它以某種方式損壞了格式。 – user1229124 2012-02-23 21:45:36

+0

對不起,regexprep(sid)格式很好,我使用的是DownloadString而不是DownloadFile,出於某種原因,有一些格式錯誤進入了csv文件中。 – user1229124 2012-02-23 21:51:22