MATLAB Answers

Locating characters in a string array

15 views (last 30 days)
Scott Dean Seedell
Scott Dean Seedell on 28 Jun 2020
Edited: dpb on 27 Oct 2020 at 21:53
Hi,
I've generated a list of numerous characters in a string (102) and assigned each character a number (1:102) in the second column to form a 102x2 string array.
What I want to do is seach for a character (column 1) and obtain the number it is assigned (column 2).
Causing me some headaches - thanks in advance!

  0 Comments

Sign in to comment.

Answers (3)

dpb
dpb on 28 Jun 2020
As the other respondent says, you don't need a corollary array unless the lookup isn't 1:1 with position (which your description implies it isn't).
The builtin functions for the purpose are strfind and contains for simple pattern matching; there's regexp for regular expressions.
See the documentation for strings, cell strings and character string arrays in the section of "Data Types" in the documentation for complete discussion of the varied ways to deal with text data in MATLAB.

  0 Comments

Sign in to comment.


RAVIKIRAN YALAMARTHI
RAVIKIRAN YALAMARTHI on 28 Jun 2020
In first step you are creating a string whose length is 102.
In second step you are creating a array of 1:102. But, in a string each character has its own index value. i.e first character index=1 and last character index=102. So, there is no need to create a another column to find corresponding number.
we can find the answer with the following example code.
x = 'Scott Dean Seedell'
y = find(x=='S')
answer will be: y = 1 12
i.e. corresponding numbers of character 'S' are 1 and 12.

  15 Comments

dpb
dpb on 15 Jul 2020
BTW, at least part of the reason for wanting to see acutal data is that I strongly suspect you should turn many (if not all) of these into categorical() variables instead of strings; much simplifying the searching.
Scott Dean Seedell
Scott Dean Seedell on 15 Jul 2020
Sorry, I didn't know that was possible.
Does the attached make it possible to see better?
Thanks
dpb
dpb on 15 Jul 2020
Well, let me go look and see...I'm sure it will. Storm last night took out the line (wind broke off a dead limb that had been intending to get down) that feeds the well so have to get finished putting it all back first...

Sign in to comment.


dpb
dpb on 16 Jul 2020
OK, got the electrical problem in stable state for time being with power back the main well so cattle are drinking...shop not on but can live without the old shop temporarily...
Anyway, I'd do something like this with your Line_Bus array but wonder why there's so much duplicated and seemingly just sequentially-numbered data. Seems like a lot of redundancy in the duplicated data department of redundancy department, but maybe this is just demo data??? That aside--
Line_bus=categorical(Line_bus); % make them all categorical() arrays
fnLocateLine=@(l) find(Line_busC(:,4)==l); % define a lookup function for shorthand
% Illustrate using lookup...
>> ix=fnLocateLine('line10') % return index of given location
ix =
9
>> Line_busC(fnLocateLine('line10'),:) % return the data associated with the index...
ans =
1×4 categorical array
8 9 9 line10
>>
It possibly could be more useful to leave the numeric columns as numeric but if they're just IDs, the categorical is probably better than string.

  8 Comments

Show 5 older comments
dpb
dpb on 1 Aug 2020
Would need a small sample test case that can reproduce the problem...can't tell anything about root cause just from the message.
It's tell you the reference is out of bounds, though, which means the array is zero-length since the row index is hardcoded as 1.
That would tend to go along with the above of an empty lookup operation.
Scott Dean Seedell
Scott Dean Seedell on 26 Oct 2020 at 20:34
Hello you.
Well I haven't done much lately on this problem (numerous other issues to worry about).
Needless to say the above issue still remains (funny enough it hasn't fixed itself).
If you're feeing helpful, can you let me know what you may need to diagnose it?
Cheers
dpb
dpb on 26 Oct 2020 at 21:33
As above, a sample test case that reproduces the issue...code and data to run it that creates the error.
I see in looking at the past conversation I said that what it was you provided before had worked for me...so that's what left me hanging--I couldn't reproduce the error with the data/code I had at hand so couldn't diagnose what was/is going wrong at your end.
So, what we need is a complete test case of code and data that produces the error so can catch it in the act...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by