sort a cell array of file paths by portions of the file path
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
HI All,
I have an output from the dirrec function, which is a 12x1 cell array, where each cell is a file path. Example below. I need to sort this array in a very specific way, because I'd like to concatenate the variables within the matfiles that have the same name (but only for the matfiles that have the same filename as specified by the paths below). I had thought that the easiest way to do this would be to sort it first by the 'Neg' or 'Pos' portion of the file path, then by the '1_50_Hz' , '50_100_Hz' etc, portion of the file path, and then to sort by the filename (within the file path). Then I would just loop through the files and concatenate. Would anyone know a quick way to do this?
Thank you!
PAK
Cell array "X":
X{1,1} = /Volumes/PAK/FR1_Local_Data/Current/UT084/Analysis_Retrieval/AH/Neg/Phase_Locking/1_50_Hz/001/NS3_001_NS6_002.mat
X{2,1} = /Volumes/PAK/FR1_Local_Data/Current/UT084/Analysis_Retrieval/AH/Neg/Phase_Locking/1_50_Hz/001/NS3_001_NS6_007.mat
X{3,1} = /Volumes/PAK/FR1_Local_Data/Current/UT084/Analysis_Retrieval/AH/Pos/Phase_Locking/1_50_Hz/001/NS3_001_NS6_006.mat'
X{4,1} = /Volumes/PAK/FR1_Local_Data/Current/UT084/Analysis_Retrieval/AH/Neg/Phase_Locking/50_100_Hz/001/NS3_001_NS6_002.mat'
X{5,1} = /Volumes/PAK/FR1_Local_Data/Current/UT084/Analysis_Retrieval/AH/Neg/Phase_Locking/50_100_Hz/001/NS3_001_NS6_007.mat'
X{6,1} = /Volumes/PAK/FR1_Local_Data/Current/UT084/Analysis_Retrieval/AH/Pos/Phase_Locking/50_100_Hz/001/NS3_001_NS6_006.mat'
X{7,1} = /Volumes/PAK/FR1_Local_Data/Current/UT084/Analysis_Retrieval/AH/Neg/Phase_Locking/100_150_Hz/001/NS3_001_NS6_002.mat'
X{8,1} = /Volumes/PAK/FR1_Local_Data/Current/UT084/Analysis_Retrieval/AH/Neg/Phase_Locking/100_150_Hz/001/NS3_001_NS6_007.mat'
X{9,1} = /Volumes/PAK/FR1_Local_Data/Current/UT084/Analysis_Retrieval/AH/Pos/Phase_Locking/100_150_Hz/001/NS3_001_NS6_006.mat'
X{10,1} = /Volumes/PAK/FR1_Local_Data/Current/UT084/Analysis_Retrieval/AH/Neg/Phase_Locking/150_200_Hz/001/NS3_001_NS6_002.mat'
X{11,1} = /Volumes/PAK/FR1_Local_Data/Current/UT084/Analysis_Retrieval/AH/Neg/Phase_Locking/150_200_Hz/001/NS3_001_NS6_007.mat'
X{12,1} = /Volumes/PAK/FR1_Local_Data/Current/UT084/Analysis_Retrieval/AH/Pos/Phase_Locking/150_200_Hz/001/NS3_001_NS6_006.mat'
0 Kommentare
Antworten (1)
Vatsal
am 11 Jun. 2024
Hi,
To concatenate variables within .mat files that share the same filename across different directories, consider utilizing a data structure to organize the file names and their associated variables. This approach bypasses the need for sorting file paths, allowing direct access to the necessary variables for concatenation.
Here is how this can be implemented:
% Initialize an empty struct to hold the file paths and their corresponding variables
filePathsAndVariables = struct();
% Loop through the cell array
for i = 1:length(X)
% Load the .mat file
matFile = load(X{i});
% Get the names of the variables in the .mat file
variableNames = fieldnames(matFile);
% Loop through the variables
for j = 1:length(variableNames)
% Get the name of the variable
variableName = variableNames{j};
% Extract the filename from the file path
[~, filename, ~] = fileparts(X{i});
% Create a key for the filename and variable name
key = [filename, '_', variableName];
% If the key already exists in the filePathsAndVariables struct,
% concatenate the new data to the existing data
if isfield(filePathsAndVariables, key)
filePathsAndVariables.(key) = [filePathsAndVariables.(key); matFile.(variableName)];
% Otherwise, add the new data to the filePathsAndVariables struct
else
filePathsAndVariables.(key) = matFile.(variableName);
end
end
end
I hope this helps!
0 Kommentare
Siehe auch
Kategorien
Mehr zu Shifting and Sorting Matrices 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!