How can I find a string in an array of strings? Error using strfind PATTERN must be a string scalar or character vector.

Dear all,
I am currently trying to derandomize a list of filenames. I do have a derandomization sheet for this. However, whenever I am trying to run the script I wrote for this I encounter the following error:
Error using strfind
PATTERN must be a string scalar or character vector.
Error in Derandomization (line 14)
idx = find(~cellfun(@isempty,strfind(N,C)))
This is my code:
clearvars
close all
basicpath = ('C:\path');
[~,~,raw] = xlsread('filenames_new_sheet.xlsx');
T = readtable('derandomization.xlsx');
t = T{:,'Filename'}
newStr = erase(t,'_kakaka'); %get body of filenames to match
C = unique(newStr) % delete filenames that exist more than once
N = raw(:,2); runr
j = size(C)
k = size(N)
for n = 1:j
for m = 1:k
idx = find(~cellfun(@isempty,strfind(N,C)))
end
end
>> class N
ans =
'char'
>> class C
ans =
'char'
Why do I keep getting this error message everytime? And what can I do to avoid it? Thank you, everyone.
Kindly

8 Comments

Why do you need for loop here?
for n = 1:j
for m = 1:k
idx = find(~cellfun(@isempty,strfind(N,C)))
end
end
To iterate through the rows of the new sheet with the string variables from the derandomization sheet
"Why do you need for loop here?"
The second input of strfind is one pattern (i.e. a single character vector or string scalar), so given that C contains multiple patterns at least one loop is required here... I guess those two loops are remants of earlier experimentation.
However, it works if I don't iterate through the columns, but if I copy/paste a particular example from the list - then I will get the fields where this particulare filename is found in the new sheet.
clearvars
close all
basicpath = ('C:\Hannah\Dystra_speech');
[~,~,raw] = xlsread('DDK_acoustic_durations.xlsx');
C2 = 'Filename_1_2_0'
N = raw(:,2);
idx = find(~cellfun(@isempty,strfind(N,C2)))
like this
idx =
2
3
4
5
6
7
8
9
10
11
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
Is there any way, I could turn my variable into a string scalar or character vector?
Thank you
If I put '' around the C it will only return empty vectors
idx =
0×1 empty double column vector
@Hannah_Mad: please upload the some sample variables (e.g. raw and T) or the original files, and please show or describe the expected output for those exact variables/files.
Hannah, again I think you misunderstood Stephen's comment. When he said "please upload the some sample variables (e.g. raw and T) or the original files" he meant to attach 'Effekte_der_DBS_korrigiert.xlsx' or 'DDK_acoustic_durations.xlsx' using the paper clip icon. And I'm not seeing those anywhere on this page on any of your posts. We'll check back later for them.

Sign in to comment.

 Accepted Answer

Try contains(). Not sure what derandomize means. Do you mean sort according to some vector of values or alphanumerically?

5 Comments

Well I have the Derandomization sheet where I have one column of the old filenames and the other ones are the derandomized numbers. And I want to finde the old filenames in the new sheet so that I can match them with the derandomization from the derandomization sheet.
So, did contains() work? If not, try ismember() if you want an exact match. If ismember() doesn't work, try swapping the first and second input arguments to ismember() and try again.
Note that ismember matches the entire string, and does not search for substrings.
Unfortunately it does not work as I want to search substrings.
But Hannah, I think you misunderstood. Stephen was merely pointing out the differences between ismember() and contains(). ismember() does not search substrings, while contains() does search substrings. So again, I ask did contains() work for you?

Sign in to comment.

Categories

Tags

Community Treasure Hunt

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

Start Hunting!