compare 1X6 cell data with Structure

1 次查看(过去 30 天)
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 个评论
Stephen23
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}, {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.

请先登录,再进行评论。

回答(2 个)

Karim
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)) ];
end
end
  5 个评论
Santosh Biradar
Santosh Biradar 2022-9-1
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 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
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.

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by