Find specific string in vector column Matlab - string

In my program I need to ask the user a string (ex: 'C4') and then obtain the corresponding index to that string on the following vector:
labels=['C2';'C#2';'D2';'D#2';'E2';'F2';'F#2';'G2';'G#2';'A2';'A#2';'B2';'C3';'C#3';'D3';'D#3';'E3';'F3';'F#3';'G3';'G#3';'A3';'A#3';'B3';'C4';'C#4';'D4';'D#4';'E4';'F4';'F#4';'G4';'G#4';'A4';'A#4';'B4';'C5';'C#5';'D5';'D#5';'E5';'F5';'F#5';'G5';'G#5';'A5';'A#5';'B5'];
To ask the string is simple, used input and it worked on octave, but to find the index I couldn't make it work. I tried using find, find(ismember( and also with a for cycle but nothing seems to work.
Appreciate the help
Thanks.

Use strcmp (and note labels should be a cell array, as the strings have different lengths):
labels = {'C2';'C#2';'D2';'D#2';'E2';'F2';'F#2';'G2';'G#2';'A2';'A#2';'B2';'C3';'C#3';'D3';'D#3';'E3';'F3';'F#3';'G3';'G#3';'A3';'A#3';'B3';'C4';'C#4';'D4';'D#4';'E4';'F4';'F#4';'G4';'G#4';'A4';'A#4';'B4';'C5';'C#5';'D5';'D#5';'E5';'F5';'F#5';'G5';'G#5';'A5';'A#5';'B5'};
str = 'C4';
index = find(strcmp(str,labels));
You can use strcmpi instead of strcmp if case is not important.

Related

Basic string slicing from indices

I will state the obvious that I am a beginner. I should also mention that I have been coding in Zybooks, which affects things. My textbook hasn't helped me much
I tried sub_lyric= rhyme_lyric[ : ]
Zybooks should be able to input an index number can get only that part of the sentence but my book doesnt explain how to do that. If it throws a [4:7] then it would output cow. Hopefully I have exolained everything well.
You need to set there:
sub_lyric = rhyme_lyric[start_index:end_index]
The string is as a sequence of characters and you can use string slicing to extract any sub-text from the main one. As you have observed:
sub_lyric = rhyme_lyric[:]
will copy the entire content of rhyme_lyric to sub_lyric.
To select only a portion of the text, specify the start_index (strings start with index 0) to end_index (not included).
sub_lyric = rhyme_lyric[4:7]
will extract characters in rhyme_lyric from position 4 (included) to position 7 (not included) so the result will be cow.
You can check more on string slicing here: Python 3 introduction

Trying to compare a string to an array and return string, instead of a value

I'm trying to write a function that looks at the string in a cell and depending on the first 2-3 letters write a string in the cell next to it.
For example:
"LSH T1402A" should return "High-Level Safety"
"FI P1402A" should return "Flow Indicator"
(I know in the second case there are only 2 symbols, but in the array I would include the space in the string so that shouldn't give any problems)
At first I was thinking of using an IF function, but quickly abandoned the idea because it would become too lengthy.(many different strings/types of sensors)
Currently I've broken down my problem in 4 steps
Read String
Return first 3 symbols
Compare to array/matrix
Write string corresponding.
The first two parts I think I can solve by using "=LEFT(TRIM([CEL]);3) but I am stuck on how to compare it to an array. The MATCH function comes close but only returns a value for which position the cell is on I believe?
Does anyone have an idea how I should continue solving this problem? Many thanks!
If your array was say in G1:H10 then you could
lookup your 3 character code in G1:G10 using MATCH
return the corresponding value from H1:H10 using INDEX
example
=INDEX(H1:H10,MATCH(LEFT(A1,3),G1:G10,0))
Function will return a #N/A if no match is found

Python how to remove the first set of a character in a string?

My variable x has a value of "000032403" and I want to remove the first set of zeros but I want to keep the other! How I gonna do that?
Note: Please give me any suggestions without knowing the amount of zeros in the beginning, because in my program this value is obtained from the user.
You can use the lstrip() function of the string class like this
>>> x = "000032403"
>>> x.lstrip("0")
"32403"
This will "return a copy of the string with leading characters removed".
Here's a link to the docs

Displaying positive symbol for positive elements in MATLAB array?

I have an array in MATLAB, and I wanted to display the positive symbol, "+" in front of positive elements, and keep the negative symbol, "-" in already existing negative values. I thought I could do the following:
I was thinking of constructing a sort of cell string or string array, and having an if, else system where if the numbers magnitude was >0, then I should store the value as '+' concatenated with the conversion of the element. If it was 0, just do a straight up char conversion since 0 has no sign, and if it was negative, just convert it. I know what to do, however, logistically, I think my order of commands is whacky.
How can I implement this?
I have the following script for an array x, but it just spews out values, I want an orderly string array I can copy and paste for use outside of MATLAB.
x;
pos = '+';
bound = length(x);
for i=1:bound
if(x(i)==0)
num2str(x(i))
end
if(x(i)>0)
num2str(x(i))
strcat(pos,num2str(x(i)))
end
if(x(i)<0)
num2str(x(i))
strcat(pos,num2str(x(i)))
end
end
I think you are searching for this.
Let's make an example.
First type in your command window :
test = 5;
Then:
sprintf('%+d',test)
You should have in this way what you want.
Of course you need to adapt it to your case. I suggest you to read this.
I hope it helps.

Can you treat a string as one object in a list in MATLAB?

I would like to make a list of strings in MATLAB using the example below:
x = ['fun', 'today', 'sunny']
I want to be able to call x(1) and have it return 'fun', but instead I keep getting 'f'.
Also, is there a way to add a string to a list without getting the list giving back a number where the string should be? I have tried using str2double and a few other things. It seems like both of these thing should be possible to do in MATLAB.
The easiest way to store a list of strings that have different lengths is to use cell arrays. For example:
>> x = {'fun', 'today', 'sunny'}; %# Create a cell array of strings
>> x{1} %# Get the string from the first cell
ans =
fun
It's kind of a kludgy workaround, but
x = strsplit('fun.today.sunny', ',')
produces a list with individual, callable strings.

Resources