Matlab以变量名保存.mat文件

问题描述:

我正在创建一个每天都在分析数据的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。我现在可以用这个工作。

感谢您的快速响应。

如果您在错误消息不起作用时提供该信息会很有帮助。

对于这种情况,我认为问题是保存的语法。你需要这样做:

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

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

documentation,使用

save(filename, variables) 

variables应当如所描述的命令时:

的变量来保存

名称,指定为一个或多个字符向量或字符串。使用save命令形式时,不需要用单引号或双引号括起输入。变量可以采用以下形式之一。

这意味着你应该使用

save(fnSave, 'fnOpen'); 

既然你还需要使用存储在一个变量文件名,命令语法是不理想的,因为你不得不使用eval。在这种情况下,替代的办法是

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

如果你有一个固定的文件名(备查),这将是简单

save C:/User/Docs/MyFile.mat fnOpen