Importing matlab data in a loop using keywords from a cell

1 次查看(过去 30 天)
Hi,
I have two cell arrays:
a1={K01 mainEEG.mat,K02 mainEEG.mat,K03 mainEEG.mat,....,K10 mainEEG.mat}
a2={K01 file1.mat',K02 file2.mat,K05 file3.mat}
Now i want to import files present in a2 from a1 using initial string as keyword i.e K01, K02 and K05. Can someone please help me with this?
  2 个评论
Walter Roberson
Walter Roberson 2016-9-4
Why do I get the sinking feeling that the output you want from this is a variable named "file1" that matches all of the filename parts of the entries whose string starts with 'K01', and a variable named "file2" that matches all of the filename parts of the entries whose string starts with 'K02', and so on?
Sowmya MR
Sowmya MR 2016-9-4
Basically K01 in both cells refer to same case with different names. The only common keyword is "K01" and so on. So i want to import all files present in a2 from a1. Hope this is clear now.

请先登录,再进行评论。

采纳的回答

Thorsten
Thorsten 2016-9-5
编辑:Thorsten 2016-9-5
a1 = {'K01 mainEEG.mat','K02 mainEEG.mat','K03 mainEEG.mat', 'K04 mainEEG.mat', 'K05 mainEEG.mat', 'K06 mainEEG.mat'}
a2 = {'K01 file1.mat', 'K02 file2.mat' , 'K05 file3.mat'}
prefix1 = cellfun(@(c) c(1:3), a1, 'UniformOutput', false)
prefix2 = cellfun(@(c) c(1:3), a2, 'UniformOutput', false)
files_I_want_to_import = a1(ismember(prefix1, prefix2))

更多回答(1 个)

dpb
dpb 2016-9-4
编辑:dpb 2016-9-5
>> c1=char(a1);c2=char(a2);
>> list=a1(ismember(c1(:,1:3),c2(:,1:3),'rows'))
list =
'K01 mainEEG.mat' 'K02 mainEEG.mat'
>>
It's a little easier to subscript the char arrays since can't dereference a substring of an array of cellstr in a single operation (or, at least, I don't know of any syntax to do so)...
NB: I just deleted the ellipses from you defining line rather than fill in the other elements so K05 was not in a1 in the above demo. Also, it's not clear precisely whether it's a1 or a2 you're actually wanting, use the appropriate one, of course...

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by