Change localization of files

2 次查看(过去 30 天)
Hi! I grouped the filenames according to a certain characteristic and now I wanted to save each file in a folder of the respective group.
I tried this but the code is not saving the files on the groups G1,G2,... in the path
G = {G1 G2 G3 G4 G5 G6};
path="C:\Users\35191\OneDrive - Universidade de Aveiro\Emotional transition - recognition\S1.mat";
cd
fileList = dir(fullfile(path));
AllFilenames = {fileList.name};
F_matrix = AllFilenames(:,3:end)';
F_matrix = convertCharsToStrings(F_matrix);
N=length(F_matrix);
% Group by name
% for i=1:6
%
% C_G{i} = find(ismember(F_matrix,G{i}));
% Group{i}=F_matrix(C_G{i});
% end
for j=1:(N-2)
cd(path)
%Read data
file=fileList(j+2).name;
load(file)
if 1i == 1
C_G{1} = find(ismember(file,G{1}));
load(file)
cd 'C:\Users\35191\OneDrive - Universidade de Aveiro\Emotional transition - recognition\G1'
save file
elseif 1i ==2
C_G{2} = find(ismember(file,G{2}));
cd 'C:\Users\35191\OneDrive - Universidade de Aveiro\Emotional transition - recognition\G2'
save file
elseif 1i == 3
C_G{3} = find(ismember(file,G{3}));
cd 'C:\Users\35191\OneDrive - Universidade de Aveiro\Emotional transition - recognition\G3'
save file
elseif 1i == 4
C_G{4} = find(ismember(file,G{4}));
cd 'C:\Users\35191\OneDrive - Universidade de Aveiro\Emotional transition - recognition\G4'
save file
elseif 1i == 5
C_G{5} = find(ismember(file,G{5}));
cd 'C:\Users\35191\OneDrive - Universidade de Aveiro\Emotional transition - recognition\G5'
save file.mat
elseif 1i == 6
C_G{6} = find(ismember(file,G{6}));
cd 'C:\Users\35191\OneDrive - Universidade de Aveiro\Emotional transition - recognition\G6'
save file
end
end
  2 个评论
Jan
Jan 2021-11-7
编辑:Jan 2021-11-7
Avoid to use the name "path" for a local variable. This is an important Matlab command and redefining it can have very strange side effects during debugging.
fullfile(path) concatenates the contents of the variable path with nothing. fullfile is not meaningful with a single input only.
F_matrix = AllFilenames(:,3:end)'
It is not documented, that . and .. are the first two elements replied by dir. Safer:
F_matrix = setdiff(AllFilenames, {'.', '..'});
But you do not use F_matrix later, so simply omit this code.
Steven Lord
Steven Lord 2021-11-7
load(file)
cd 'C:\Users\35191\OneDrive - Universidade de Aveiro\Emotional transition - recognition\G1'
save file
This loads the file whose name is stored in the variable named file, changes directory, then saves a file named file (not a file whose name is stored in the variable named file.)
save(X) % Save to the file whose name is stored in the variable x
save X % Save to the file whose name is X, equivalent to
save('X') % Save to the file whose name is X

请先登录,再进行评论。

采纳的回答

Jan
Jan 2021-11-7
编辑:Jan 2021-11-7
if 1i == 1
This not true in any case. 1i is the imaginary unit. sqrt(-1) is not equal to 1 in any case. It is not 2,3,4,5,6 also. So non of the if branches will be entered.
Do you mean "j" instead of "1i"?
What is the purpose of the code? "save file" stores all variables of the current workspace in a file called "file.mat". Is this useful?
What is the contents of G1, G2, ...?
I assume, that the code can be simplified. Maybe:
BaseFolder = ['C:\Users\35191\OneDrive - Universidade de Aveiro\', ...
'Emotional transition - recognition'];
data = load(fullfile(BaseFolder, 'S1.mat');
fileList = dir(fullfile(BaseFolder, '*.mat'));
fileList = setdiff(fileList, {'.', '..'});
for k = 1:6
... % Something which uses the cell G?!
file = fileList(k);
save(fullfile(BaseFolder, sprintf('G%d', k), file), '-struct', 'data');
end
  3 个评论
Jan
Jan 2021-11-8
Is "S1.mat" a diretory?
What are "sequences"?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by