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

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
adi kul 2019년 12월 3일
Fixed it. thanks.
But facing same error!
Stephen23
Stephen23 2019년 12월 3일
편집: Stephen23 2019년 12월 3일
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
adi kul 2019년 12월 3일
nope. This is different code.
The "myPath" is just an example. actual path is C:\... something like this
Stephane
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
Thing is if I use fullFileName instead of outputFullFilename it indeed saves same file with added variable.
% outputBaseFileName = sprintf('Edited_%s', baseFileName);
% outputFullFileName = fullfile(file(k).folder, outputBaseFileName);
save(fullFileName,'Variable_1','-append');
I am not sure what's wrong in here. Here is the full code here:
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)
Variable_1=x.Variable_4./x.Varibale_5;
outputBaseFileName = sprintf('Edited_%s', baseFileName);
outputFullFileName = fullfile(file(k).folder, outputBaseFileName);
save(outputFullFileName,'Variable_1','-append');
end

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2019년 12월 4일

0 개 추천

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

추가 답변 (1개)

Ruger28
Ruger28 2019년 12월 3일
편집: Ruger28 2019년 12월 3일

0 개 추천

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

adi kul
adi kul 2019년 12월 4일
this creates Edited_ .mat file but with only Variable_1 in it. While I want to have all the other variables from oriniglal .mat file and apend with Variable_1

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Filename Construction에 대해 자세히 알아보기

태그

질문:

2019년 12월 3일

답변:

2019년 12월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by