Importing matlab data in a loop using keywords from a cell

1 view (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 4 Sep 2016
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 on 4 Sep 2016
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.

Sign in to comment.

Accepted Answer

Thorsten
Thorsten on 5 Sep 2016
Edited: Thorsten on 5 Sep 2016
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))

More Answers (1)

dpb
dpb on 4 Sep 2016
Edited: dpb on 5 Sep 2016
>> 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...

Categories

Find more on Large Files and Big Data 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!