Importing matlab data in a loop using keywords from a cell

조회 수: 1 (최근 30일)
Sowmya MR
Sowmya MR 2016년 9월 4일
편집: Thorsten 2016년 9월 5일
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에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by