Saving mat file with prefix over loop
1 次查看(过去 30 天)
显示 更早的评论
Hello,
Looking help in saving same .mat file with prefix over loop.
Facing error with following script:
clear all
close all
d = uigetdir();
filePattern = fullfile(d, '*.mat');
file = dir(filePattern);
for k = 1: numel(file)
baseFileName = file(k).name;
fullFileName = fullfile(d, baseFileName);
% my code here
outputBaseFileName = sprintf('Edited_%s', baseFileName);
outputFullFileName = fullfile(file(k).folder, outputBaseFileName);
save(outputFullFileName,'Variable_1','-append');
end
Error I am getting is:
Error using save
Unable to write file 'myPath\Edited_myFileName.mat'
No such file or directory.
Error in Untitled (line 29)
save(outputFullFileName,'Variable_1','-append');
6 个评论
Stephane
2019-12-3
You can cerate a dummy text file where you want to save your files, then compare its path+name to the output of outputFullFileName
采纳的回答
Walter Roberson
2019-12-4
clear all
close all
d = uigetdir();
filePattern = fullfile(d, '*.mat');
file = dir(filePattern);
for k = 1: numel(file)
baseFileName = file(k).name;
fullFileName = fullfile(d, baseFileName);
x=load(fullFileName)
x.Variable_1 = x.Variable_4./x.Varibale_5;
outputBaseFileName = sprintf('Edited_%s', baseFileName);
outputFullFileName = fullfile(file(k).folder, outputBaseFileName);
save(outputFullFileName, '-struct', 'x');
end
0 个评论
更多回答(1 个)
Ruger28
2019-12-3
编辑:Ruger28
2019-12-3
Your Edited .mat file does not exist, so you must create it. Check to see if it exists, if it does, append. If it does not, create it.
clear all
close all
d = uigetdir();
filePattern = fullfile(d, '*.mat');
file = dir(filePattern);
for k = 1: numel(file)
% my code here
outputFullFileName = fullfile(file(k).folder,sprintf('Edited_%s', file(k).name));
if exist(outputFullFileName,'file') % file exists
save(outputFullFileName,'Variable_1','-append');
else % file is new since you made it "edited", and needs created
save(outputFullFileName,'Variable_1');
end
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Filename Construction 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!