compare 1X6 cell data with Structure

1 view (last 30 days)
Santosh Biradar
Santosh Biradar on 31 Aug 2022
Edited: Stephen23 on 1 Sep 2022
Hello
I have NameBLFFile Cell and Matfile stuct.
k>>NameBLFFile =
1x6 cell array
{'20220329_112048_ADU_INPUT'} {'20220329_112048_ADU_INPUT'} {'20220329_141153_ADU_INPUT'} {'20220329_142653_ADU_INPUT'} {'20220329_144153_ADU_INPUT} {'20220329_160005_ADU_INPUT'}
K>>MatFiles =
A 20x1 struct array with fields:
name
folder
date
bytes
isdir
datenum
K>> MatFiles.folder
ans = 'D:\ninn\2_resimResult_0628\20220329\20220329_110545_ADU_INPUT'
ans = 'D:\ninn\2_resimResult_0628\20220329\20220329_112048_ADU_INPUT'
ans = 'D:\ninn\2_resimResult_0628\20220329\20220329_132650_ADU_INPUT'
ans = 'D:\ninn\2_resimResult_0628\20220329\20220329_134153_ADU_INPUT'
ans = 'D:\ninn\2_resimResult_0628\20220329\20220329_135653_ADU_INPUT'
ans = 'D:\ninn\2_resimResult_0628\20220329\20220329_141153_ADU_INPUT'
ans = 'D:\ninn\2_resimResult_0628\20220329\20220329_142653_ADU_INPUT'
ans = 'D:\ninn\2_resimResult_0628\20220329\20220329_144153_ADU_INPUT'
ans = 'D:\ninn\2_resimResult_0628\20220329\20220329_154502_ADU_INPUT'
ans = 'D:\ninn\2_resimResult_0628\20220329\20220329_160005_ADU_INPUT'
ans = 'D:\ninn\2_resimResult_0628\20220329\20220329_161505_ADU_INPUT'
ans = 'D:\ninn\2_resimResult_0628\20220329\20220329_163005_ADU_INPUT'
ans = 'D:\ninn\2_resimResult_0628\20220329\20220329_170848_ADU_INPUT'
ans = 'D:\ninn\2_resimResult_0628\20220329\20220329_172351_ADU_INPUT'
ans = 'D:\ninn\2_resimResult_0628\20220329\20220329_173851_ADU_INPUT'
ans = 'D:\ninn\2_resimResult_0628\20220329\20220329_175351_ADU_INPUT'
ans = 'D:\ninn\2_resimResult_0628\20220329\20220329_180851_ADU_INPUT'
ans = 'D:\ninn\2_resimResult_0628\20220329\20220329_183851_ADU_INPUT'
ans = 'D:\ninn\2_resimResult_0628\20220329\20220329_185351_ADU_INPUT'
ans ='D:\ninn\2_resimResult_0628\20220329\20220329_190851_ADU_INPUT'
In a result, I need to compare NameBLFFile Cell and Matfile.Folder name.
ifselPath
cd(selPath);
MatFiles = dir('**/*_Sfunc.mat');
AviFiles = dir('**/*_CAM_Front.avi');
%% Here I want to put a logic where, it will pass only those .mat file where the NameBLFFile's cell is present.
for i=1:length(MatFiles)
FilesFolder = [FilesFolder,convertCharsToStrings(MatFiles(i).folder)];
AviFilesName = [AviFilesName,convertCharsToStrings(AviFiles(i).name)];
MatFilesName = [MatFilesName,convertCharsToStrings(MatFiles(i).name)];
AviFilesPath = [AviFilesPath,strcat(FilesFolder(i),'\',AviFilesName(i))];
MatFilesPath = [MatFilesPath,strcat(FilesFolder(i),'\',MatFilesName(i))];
end
Please let me know for brief.
I have attached Matfile Image for data file analysis.
Thank you
  1 Comment
Stephen23
Stephen23 on 31 Aug 2022
Edited: Stephen23 on 1 Sep 2022
Get rid of that complicated loop. Use FULLFILE rather than string concatenation.
Simpler and more efficient:
AviFilesPath = string(fullfile({AviFiles.folder}, {AviFiles.name}))
MatFilesPath = string(fullfile({MatFiles.folder}, {MatFiles.name}))
Using CD in code is slow and makes debugging harder. Much better to use absolute/relative paths instead.

Sign in to comment.

Answers (2)

Karim
Karim on 31 Aug 2022
Edited: Karim on 31 Aug 2022
you can use the contains function do determine if the desired name is in the folder name. See below for the procedure. The result is a logical array, you can use this in the loop.
EDIT: I modified the answer a bit to account for the improvment suggested by @Stephen23 to make it more robust
% create a string array with the select mat files
NameBLFFile = ["20220329_112048_ADU_INPUT" ; "20220329_112048_ADU_INPUT" ; "20220329_141153_ADU_INPUT" ; "20220329_142653_ADU_INPUT" ; "20220329_144153_ADU_INPUT" ; "20220329_160005_ADU_INPUT" ];
% recreate the dir command ouput given by the OP
MatFiles( 1).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_110545_ADU_INPUT';
MatFiles( 2).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_112048_ADU_INPUT';
MatFiles( 3).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_132650_ADU_INPUT';
MatFiles( 4).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_134153_ADU_INPUT';
MatFiles( 5).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_135653_ADU_INPUT';
MatFiles( 6).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_141153_ADU_INPUT';
MatFiles( 7).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_142653_ADU_INPUT';
MatFiles( 8).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_144153_ADU_INPUT';
MatFiles( 9).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_154502_ADU_INPUT';
MatFiles(10).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_160005_ADU_INPUT';
MatFiles(11).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_161505_ADU_INPUT';
MatFiles(12).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_163005_ADU_INPUT';
MatFiles(13).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_170848_ADU_INPUT';
MatFiles(14).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_172351_ADU_INPUT';
MatFiles(15).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_173851_ADU_INPUT';
MatFiles(16).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_175351_ADU_INPUT';
MatFiles(17).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_180851_ADU_INPUT';
MatFiles(18).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_183851_ADU_INPUT';
MatFiles(19).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_185351_ADU_INPUT';
MatFiles(20).folder = 'D:\ninn\2_resimResult_0628\20220329\20220329_190851_ADU_INPUT';
% convert the struct into a cell array
MatFiles_folder = {MatFiles.folder};
% convert the char array into a string array
MatFiles_folder = string( MatFiles_folder );
% look if the folder contains the mat file
FolderIdx = contains(MatFiles_folder, NameBLFFile)
FolderIdx = 1×20 logical array
0 1 0 0 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0
MatFiles = dir('**/*_Sfunc.mat');
AviFiles = dir('**/*_CAM_Front.avi');
for i = 1:length(MatFiles)
% now use the logical vector to determine wheter or not to process
if FolderIdx(i)
FilesFolder = [FilesFolder, convertCharsToStrings(MatFiles(i).folder) ];
AviFilesName = [AviFilesName, convertCharsToStrings(AviFiles(i).name) ];
MatFilesName = [MatFilesName, convertCharsToStrings(MatFiles(i).name) ];
AviFilesPath = [AviFilesPath, strcat(FilesFolder(i),'\',AviFilesName(i)) ];
MatFilesPath = [MatFilesPath, strcat(FilesFolder(i),'\',MatFilesName(i)) ];
end
end
  5 Comments
Santosh Biradar
Santosh Biradar on 1 Sep 2022
Thank you for being more specific.
I will go through it.
Before that I have one more thing to ask,
with this logic can I read and store multiple MAT files and it's data.
For eg. this is the folder,
Inside this, there is one AVI file and one MAT file.
So can I store data with below logic.
Will it over write my work space for everynext loop. I could not confirm. Sorry if it is not very basic question.
for i=1:length(MatFiles)
FilesFolder = [FilesFolder,convertCharsToStrings(MatFiles(i).folder)];
AviFilesName = [AviFilesName,convertCharsToStrings(AviFiles(i).name)];
MatFilesName = [MatFilesName,convertCharsToStrings(MatFiles(i).name)];
AviFilesPath = [AviFilesPath,strcat(FilesFolder(i),'\',AviFilesName(i))];
MatFilesPath = [MatFilesPath,strcat(FilesFolder(i),'\',MatFilesName(i))];
end
Thank you
Karim
Karim on 1 Sep 2022
I wanted to demonstrate how to create a logical array using the contains function and blindly copied the loop from you question. However, as @Stephen23 indicates there is a good chance that the 2 structs are not the same (even if the dimensions are the same, the order could be different).
Stephens provided and answer (here) that accounts for the possibility of different structs (using endswith) so i wont update my answer.

Sign in to comment.


Stephen23
Stephen23 on 1 Sep 2022
Edited: Stephen23 on 1 Sep 2022
It is unclear from your question, if both MatFiles structures are one and the same, or are different.
Note that your approach does not check that results returned by both DIR call actually correspond with each other.
In any case, you need to get rid of the loop and simplify your code. As far as I can tell, something like this:
idx = endsWith({MatFiles.folder}, NameBLFFile);
MatFiles = dir(fullfile(selPath,'**','*_Sfunc.mat'));
AviFiles = dir(fullfile(selPath,'**','*_CAM_Front.avi'));
AviFilesPath = string(fullfile({AviFiles(idx).folder}, {AviFiles(idx).name}))
MatFilesPath = string(fullfile({MatFiles(idx).folder}, {MatFiles(idx).name}))
or, if you want to filter the entire structures:
MatFiles = MatFiles(idx);
AviFiles = AviFiles(idx);
Using CD in code is slow and makes debugging harder. Much better to use absolute/relative paths instead.

Categories

Find more on Cell Arrays in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!