Saving mat file with prefix over loop
Ältere Kommentare anzeigen
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 Kommentare
Walter Roberson
am 3 Dez. 2019
outputFullFileName = fullfile(file(k).folder,, outputBaseFileName);
is not valid syntax: it has two commas in a row.
Did the error message literally give the path starting with myPath\ or did you edit that for posting purposes?
adi kul
am 3 Dez. 2019
It looks like you are actually using code from your earlier question:
You need to define D as an existing absolute path or relative path, instead of using the literal text 'mypath' (which apparently does not exist).
adi kul
am 3 Dez. 2019
Stephane
am 3 Dez. 2019
You can cerate a dummy text file where you want to save your files, then compare its path+name to the output of outputFullFileName
adi kul
am 4 Dez. 2019
Akzeptierte Antwort
Weitere Antworten (1)
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
1 Kommentar
adi kul
am 4 Dez. 2019
Kategorien
Mehr zu Filename Construction finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!