2017-08-13 79 views
0

我正在創建一個每天都在分析數據的matlab應用程序。 數據從一個csv文件使用xlsread讀入()Matlab以變量名保存.mat文件

[num, weather, raw]=xlsread('weather.xlsx'); 
% weather.xlsx is a spreadsheet that holds a list of other files (csv) i 
% want to process 
for i = 1:length(weather) 
    fn = [char(weather(i)) '.csv']; 

    % now read in the weather file, get data from the local weather files 
    fnOpen = xlsread(fn); 

    % now process the file to save out the .mat file with the location name 
    % for example, one file is dallasTX, so I would like that file to be 
    % saved as dallasTx.mat 
    % the next is denverCO, and so denverCO.mat, and so on. 
    % but if I try... 
    fnSave=[char(weather(i)) '.mat'] ; 
    save(fnSave, fnOpen) % this doesn't work 

    % I will be doing quite a bit of processing of the data in another 
    % application that will open each individual .mat file 
end 

++++++++++++++ 抱歉不提供全部信息。 我在執行上述操作時遇到的錯誤是: 使用保存時出錯 參數必須包含一個字符串。

和祥汝和Wolfie,save(fnSave,'fnOpen')按照您的建議工作。現在我有一個dallasTX.mat文件,裏面的變量名是fnOpen。我現在可以用這個工作。

感謝您的快速響應。

回答

1

如果您在錯誤消息不起作用時提供該信息會很有幫助。

對於這種情況,我認爲問題是保存的語法。你需要這樣做:

save(fnSave, 'fnOpen'); % note the quotes 

此外,您可以使用weather {i}而不是char(weather(i))。

0

documentation,使用

save(filename, variables) 

variables應當如所描述的命令時:

的變量來保存

名稱,指定爲一個或多個字符向量或字符串。使用save命令形式時,不需要用單引號或雙引號括起輸入。變量可以採用以下形式之一。

這意味着你應該使用

save(fnSave, 'fnOpen'); 

既然你還需要使用存儲在一個變量文件名,命令語法是不理想的,因爲你不得不使用eval。在這種情況下,替代的辦法是

eval(['save ', fnSave, ' fnOpen']); 

如果你有一個固定的文件名(備查),這將是簡單

save C:/User/Docs/MyFile.mat fnOpen 
相關問題