Matlab is returning error "Argument must contain a string." when using "save"
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Thumree Sarkar
am 27 Dez. 2018
Bearbeitet: Stephen23
am 27 Dez. 2018
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 Kommentare
Stephen23
am 27 Dez. 2018
Bearbeitet: Stephen23
am 27 Dez. 2018
" ...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.
Akzeptierte Antwort
madhan ravi
am 27 Dez. 2018
Bearbeitet: madhan ravi
am 27 Dez. 2018
See
Best use
sprintf('Files_%d.mat',j) % instead of strcat
7 Kommentare
Weitere Antworten (1)
Walter Roberson
am 27 Dez. 2018
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 Kommentare
Walter Roberson
am 27 Dez. 2018
Bearbeitet: Walter Roberson
am 27 Dez. 2018
i suspect that your pp might be a cell array.
Siehe auch
Kategorien
Mehr zu Environment and Settings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!