Matlab is returning error "Argument must contain a string." when using "save"

Hi,
I am trying to save some 3D variables in different named .mat files. I have created the filenames and variables in for loop. I am putting the files in cell (some cells wil be empty, I have some more for loops and if cnditions in between which I have not showed in the sample code) so that I can save them later. Matlab is giving me the error "Argument must contain a string.". I tried saving the files in the for loop previously but Matlab gave the same error.
I have tried running the code for a single year also, with the same error msg.
If the filenames are not variale then the save option is working fine, but I need to vary the filenames as they are representative of the year.
Please help me to save the variables.
path='somepath';
data=dir('somepath\*.TXT');
years={'2007';'2008';'2009';'2010';'2011';'2012';'2013';'2014';'2015';'2016'};
for i=1:size(data,1)
addpath(fullfile(path));
for j=1:size(years,1)
filenames=data(i).name;
filestring=cellstr(filenames);
xx=regexp(filestring,'\d','Match');
totreg=cellstr(xx{:});
pp=strcat(totreg(1),totreg(2),totreg(3),totreg(4));
year=str2double(pp);
if year == str2double(years(j));
filenames
'found the file'
if mod(str2double(years(j)), 4) == 0 && mod(str2double(years(j)), 100) ~= 0
'status = true2'
fileread2=dlmread(filenames,'',0,1);
Data_rain_leap2(:,:,:)=reshape(fileread2, [366,130,135]);
Data_rain_2{j}=Data_rain_leap2(:,2:end,:);
outputfilename2{j}=strcat('Data_',pp,'.mat');
addpath(fullfile(path));
%save(outputfilename2(j),'Data_rain_2'); it is also not working
else
'status = false'
fileread3=dlmread(filenames,'',0,1);
Data_rain_leap3(:,:,:)=reshape(fileread3, [365,130,135]);
Data_rain_3{j}=Data_rain_leap3(:,2:end,:);
outputfilename3{j}=strcat('IMD_grid_',pp,'.mat');
end
end
end
end
addpath(fullfile(path));
xx=find(~cellfun(@isempty,Data_rain_3));
Data_rain_3XX=Data_rain_3{xx};
save(outputfilename3{xx},'Data_rain_3XX');

5 件のコメント

Stephen23
Stephen23 2018 年 12 月 27 日
編集済み: Stephen23 2018 年 12 月 27 日
The purpose of this loop is not clear:
for j= 1:size(years)
Data{j}=Data_1(:,2:end,:);
outputfilename{j}=strcat('Files_',pp,'.mat');
end
On every iteration you copy data from the same location of the same variable Data_1, so you just end up with multiple copies of the same data, which you then want to save in multiple files. Why do you want to save exactly the same data in multiple files?
Please upload the data variable Data_1 in a .mat file.
Thumree Sarkar
Thumree Sarkar 2018 年 12 月 27 日
Hi,
Thanks for your time, I have now given the full code so that you understand why I have used the loop (j).
I did try to save the data within the loop itself, as the error was there so thought may be storing the variables in cell and saving them later will be better. Though the same error appeared.
I dont want to save the same data in multiple files, in each iteration a new file will be generated and I want them t be saved in seperate files, with the years name on the flename.
Stephen23
Stephen23 2018 年 12 月 27 日
編集済み: Stephen23 2018 年 12 月 27 日
@Thumree Sarkar: you really need to revise your code. For a start, you should NOT change the Search Path just to read/write data files (and also in the line fullfile is totally superfluous):
addpath(fullfile(path));
Instead, it is much better to just use absolute/relative filenames. A lot of your code is curiously complex, e.g. given that data(i).name returns one filename, this
filenames = data(i).name;
filestring = cellstr(filenames);
xx = regexp(filestring, '\d', 'Match');
totreg = cellstr(xx{:});
pp = strcat(totreg(1), totreg(2), totreg(3), totreg(4));
year = str2double(pp);
could probably be simplified by using a more suitable regular expression:
year = str2double(regexp(data(i).name,'\d{4}','match','once'))
Probably that could even be moved before the loop, and the comparison against years be made using setdiff or something similar.
Thumree Sarkar
Thumree Sarkar 2018 年 12 月 27 日
Thanks for the suggestion.
I know the part with extracting the year in filename could be much simpler but when in practice was returning some error. I will use your suggested modification.
For the use of path, I have seen sometimes, if I do not redirect the path the code is not able to find the folders/write in the specific folders, (As I have too many data and too many folders). So I use that.
I will surely keep your suggestions in mind and try to make my codes more efficient and simpler in future.
Stephen23
Stephen23 2018 年 12 月 27 日
編集済み: Stephen23 2018 年 12 月 27 日
" ...if I do not redirect the path the code is not able to find the folders/write in the specific folders"
All MATLAB functions that read/write data files accept absolute/relative filenames, so there is absolutely no need to change the Search Path (or to change the current directory) just to access data files. Using an absolute/relative filepath is more efficient and easier to debug too.

サインインしてコメントする。

 採用された回答

madhan ravi
madhan ravi 2018 年 12 月 27 日
編集済み: madhan ravi 2018 年 12 月 27 日

7 件のコメント

Walter Roberson
Walter Roberson 2018 年 12 月 27 日
The %d in the sprintf requires a numeric argument for input
madhan ravi
madhan ravi 2018 年 12 月 27 日
Yes sir you are right ... just saw your comment.
Thumree Sarkar
Thumree Sarkar 2018 年 12 月 27 日
Thank you for the time, but unfortunately I have seen the links before and tried each one of them which failed. I have previously used this 'save' command in for loop which worked fine, I do not know how this time it got stuck, may be becase of the final variables cominc out of a 'if' condition. I have edited my question and given a bigger code sample of mine, please see where I am doing wrong.
Thumree Sarkar
Thumree Sarkar 2018 年 12 月 27 日
Hi,
I need the years written on the respective filenames, the sprint f option is not providing that.
madhan ravi
madhan ravi 2018 年 12 月 27 日
years=2007:2016;
for j = 1:numel(years)
sprintf('File_%d.mat',years(j))
end
Thumree Sarkar
Thumree Sarkar 2018 年 12 月 27 日
Thank you very much for the help. sprintf works fine and the problem with 'save' is also not there. Thanks a loottt.
madhan ravi
madhan ravi 2018 年 12 月 27 日
Anytime :)

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 12 月 27 日
save(outputfilename{xx},'Data{xx}');
You cannot use an expression for the name of the variable to save. You would need to do something like
Dataxx = Data{xx};
save(outputfilename{xx}, 'Dataxx');
If you did, then the name of the variable that got stored in the .mat file would be Dataxx . If you need the variable name to be dynamic, then there is a different approach that you would need to use.

3 件のコメント

Thumree Sarkar
Thumree Sarkar 2018 年 12 月 27 日
This approach also gives the same error.
Walter Roberson
Walter Roberson 2018 年 12 月 27 日
編集済み: Walter Roberson 2018 年 12 月 27 日
i suspect that your pp might be a cell array.
Thumree Sarkar
Thumree Sarkar 2018 年 12 月 27 日
No, actually ''pp'' is a string. Btw, my problem is solved now, I have accepted the answer by Mr Ravi, I think strcat was making some issue :(. Thanks for your time :)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

質問済み:

2018 年 12 月 27 日

編集済み:

2018 年 12 月 27 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by