compare 1X6 cell data with Structure

조회 수: 1(최근 30일)
Santosh Biradar
Santosh Biradar 2022년 8월 31일
편집: Stephen23 2022년 9월 1일
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:
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.
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))];
Please let me know for brief.
I have attached Matfile Image for data file analysis.
Thank you
  댓글 수: 1
Stephen23 2022년 8월 31일
편집: Stephen23 2022년 9월 1일
Get rid of that complicated loop. Use FULLFILE rather than string concatenation.
Simpler and more efficient:
AviFilesPath = string(fullfile({AviFiles.folder}, {}))
MatFilesPath = string(fullfile({MatFiles.folder}, {}))
Using CD in code is slow and makes debugging harder. Much better to use absolute/relative paths instead.

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


Karim 2022년 8월 31일
편집: Karim 2022년 8월 31일
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)) ];
  댓글 수: 5
Karim 2022년 9월 1일
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.

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

Stephen23 2022년 9월 1일
편집: Stephen23 2022년 9월 1일
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.


Find more on Data Type Identification in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by