Locating characters in a string array
Show older comments
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!
Answers (3)
dpb
on 28 Jun 2020
1 vote
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.
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
Scott Dean Seedell
on 28 Jun 2020
>> x = 'Scott Dean Seedell';
>> strfind(x,'S')
ans =
1 12
>>
Image Analyst
on 29 Jun 2020
Is the assigned number you gave it just it's index (location) in the character array, or is it some independent number unrelated to its index?
Scott Dean Seedell
on 12 Jul 2020
dpb
on 12 Jul 2020
Show more detail of your actual use case -- including the precise storage using.
Scott Dean Seedell
on 13 Jul 2020
Edited: dpb
on 13 Jul 2020
dpb
on 13 Jul 2020
That looks awfully complicated -- can you provide a copy of (I guess it would be variable K that you're building this array out of?
Is DSSText.Command a property or what? It doesn't seem it has much connection to anything else...unless the .BusNames method(?) uses it behind the scenes, mayhaps???
dpb
on 14 Jul 2020
As noted before, sample data and small demos are the best way to illustrate -- so folks don't have to try to make up something to play with (and that may after the effort, not reflect the real problem).
"MAKE IT EASY FOR US TO HELP YOU!!!!
Scott Dean Seedell
on 14 Jul 2020
dpb
on 14 Jul 2020
As noted before, sample data and small demos are the best way to illustrate -- so folks don't have to try to make up something to play with (and that may after the effort, not reflect the real problem).
"MAKE IT EASY FOR US TO HELP YOU!!!!
Scott Dean Seedell
on 14 Jul 2020
dpb
on 14 Jul 2020
A set of the data you have as you have it is all we need...use paperclip and attach a file or just cut and paste some text...
A start would be
save seedall Line_list K line_bus
will leave you a file 'seedall.mat" with the content of those variables. Attach it (use paperclip icon) and then we can see EXACTLY what you're looking at instead of continuing to guess.
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
on 15 Jul 2020
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...
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.
10 Comments
Scott Dean Seedell
on 16 Jul 2020
dpb
on 16 Jul 2020
Glad to help...what I wasn't able to decipher before was that you really didn't need a search within a string but just for a string.
BTW, if for some reason there's a real reason to use the data as string instead of categorical owing to processing later in some manner, you can use contains the same was as above to return a logical array which can turn into index via find.
SIDEBAR:
Turns out the old panel box (and we're talking I don't have any idea how old, but back in the days of cloth-insulated wire) apparently also gave up the ghost and was shorting across its main so I couldn't turn power back on to the shop. Debugging symptoms today revealed that in the end...so, I've now got a new breaker box in and wire pulled--it does provide power in again w/o the feed breaker tripping so that fault has been removed. I'll go put the breakers in and wire up the inside circuits here in a little while -- that will let me do them one-by-one, though, if there any faults have sprung up inside with having handled that old wire...not exactly what had planned for this week!!!
Scott Dean Seedell
on 20 Jul 2020
dpb
on 21 Jul 2020
Works for me...not sure what your use case was/is that causes a problem.
You did remember to either use a try...catch construct or test for an empty result if the match fails because you've asked for a non-present category, correct? In that case you get an empty result which would fail on a subsequent attempt to use as an array index.
Post an actual failure example with accompanying error messge if that isn't the problem...
Scott Dean Seedell
on 1 Aug 2020
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
on 26 Oct 2020
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...
Scott Dean Seedell
on 10 Nov 2020
dpb
on 11 Nov 2020
MATLAB surface plots are pretty restrictive -- must have X,Y data in meshgrid form with a corresponding Z 2D matrix; it doesn't have the facilities to handle columnar Z (as you've undoubtedly discovered)
You would have to create a meshgrid and fill in with interpolation to use those; however, you can visualize the data with scatter3 which will accept the vector form--doing that for your data results in:

There's really no evidence of any surface at all, anyway -- I then followed up with the marginal plots of Z vs X and Y alone and they also are basically random with only a slight preponderance to more values centered around Z=3E4 than higher, but still there's no indication of any real pattern that a surface would fit.
Categories
Find more on Characters and Strings 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!