matlab divide sentences into words - arrays

Im new in matlab and ım trying to take the input from matlab gui which will be entered by a user and divide that sentence into words but I need to have them as letters because Im using a robot to write them. this letters will be send to these robots. Im using two robots and for example when I write 'lou reed' in text when ı press the button matlab function will hold this 2 words in to different char arrays so that ı can have the letters c(i) like this and send them to process. so far ı wrote these but ım stuck.
c = char(get(handles.edit1,'String'));
int count1;
int count2;
char word1;
char space=" ";
for i=1:length(c)
int t = isequal(c(i),space);
if(t==0)
count1=count1+1;
word1=;%ım trying to add the char here to find the new word
else
end
end
ı dont know what to do ı searched but ı couldnt find something usefull maybe ı wasnt looking right.
Anything would be helpful, thankss

What characters are allowed? First you should remove all the characters that are not allowed (substitute them with a space character?). After that just this:
str = ' Once upon a time ';
words_in_str = textscan(str,'%s');
words_in_str{1}

If you have a newer version of MATLAB (greater than 2012a I think), you can use strsplit
characterString = 'lou reed';
C = strsplit(characterString);
C will be a cell array with each element being a separate word.

You can simply find the space characters in your string with
mystring = 'Hello Cruel World';
spaces = find(mystring==' ');
The variable spaces is now a vector pointing to where each of your word breaks are. If you want to break this up into words, you could use
mystring = 'Hello Cruel World';
wordboundaries = [0,find(mystring==' ')];
wordlen = diff([wordboundaries,length(mystring)+1])-1;
numwords = length(wordboundaries);
for w = 1:numwords
idx = wordboundaries(w) + (1:wordlen(w));
word{w} = mystring(idx);
end
display(word);
Now word is a cell array containing the individual words.

Related

Replacing specific letters from a string array

I have the following array
char myArray[] = {"H4w6m1ny6pr4gr1mm2rs6d42s63t6t1k26t46ch1ng2616l3ght6b5lb?6N4n2,6th1t’s616h1rdw1r26pr4bl2m"};
and want to replace specific characters from it, H to 1, or all m's to J.
I know I have to use a loop, but how do I do to replace multiple letters with whatever I want? I could go and myArray[0] = 'J' but replacing each position individually doesn't seem efficient to me. Any ideas?

C# WinForm: Select specific words and change colour

I have a program that allows the user to enter text, and it will highlight any repeat words. It already adds repeated words to one list and all the words to another. I want the program to print out the words, and if a repeat word is used, to highlight it.
I have tried using outputBox.Find(repeatList[i]) with a loop, but this only finds the first word used in the text. I also tried marking the current number for the last letter typed, selecting that point, finding the coordinates after the word.Length was typed, and then changing the colour of that, which didn't work.
for (int h = 0; h < repeatList.Count; h++)
{
for (int c = 0; c < repeatList.Count; c++)
{
outputBox.Find(repeatList[h]);
outputBox.SelectionColor = Color.Red;
}
}
At this point in the code, the outputBox already contains the users input, I just want to know how to compare words and select them for colouring. I've only just started Winforms and have only been coding for a few weeks, so I am sorry - I have looked at other answers but was not able to implement them. Thank you in advance for any responses.
EDIT: I would just like to add that my prefered method for colouring the text would be as it prints each word out, this was my original intention as I'm much more used to console applications where I can just change the colour and print more. If that method is easier to do than checking afterwards, I'm find to do that.
I would use the start index and keep a copy of it.
int startFrom = 0
...
startFrom = outputBox.Find(repeatList[h], startFrom)
You could then use the 'startFrom' index with the text word length to select the text.
Here is an example:
var findText = "test";
int index = 0;
do
{
index = richTextBox1.Find(findText, index, RichTextBoxFinds.WholeWord);
if (index > -1)
{
richTextBox1.Select(index, findText.Length);
richTextBox1.SelectionColor = Color.Red;
index++;
}
} while (index > -1);

How to split sentences in an array

I have a string s which stores a very long sentence and I want to copy the content of s to an array C with each cell storing a sentence each. The following is my code which is not giving me any output, but the dimension of the cell:
while(i<6)
C(i)=s;
end
This is how I get as output when I print C:
C=
[1x76 char]
Can somebody please help me.
Another job for strsplit:
>> sentences = 'This is the first one. Then here is a second. Yet another here.';
>> C = strsplit(sentences,'. ')
C =
'This is the first one' 'Then here is a second' 'Yet another here.'
We are specifying a period followed by a space as the delimiter. Change this as needed.
Suppose Long string is:
longString = "This is first cell. This is second cell. this is third cell".
Now since . is delimiter here means it is acting as separator for sentences. so you can loop through longString character wise and whenever you encounter a . you just increase Array index count and keep storing in this Array index until you find another .
here is sudo code:
array[];
index = 0;
loop through(longString) character wise
{
if(currentChar equals to '.')
{
index++;
}
else
{
array[index] = currentChanracter;
}
}

How can I find words from scrambled letters?Visual Basic 2010

I have scrambled the letters, so they're different each time and have a txt file which contains all of the words from the dictionary. I am new to this and trying to teach myself VB, but this has got me, so could really do with some help.
Basically, how can I make sure that what the player enters into the textbox is a word that can be found in the scrambled 8 letters by comparing against the string and also the txt file to check that it is a real word? The words can be any length, as long as they're less than 8.
I have two arrays, one for consonants and one for vowels, so the player creates the scrambled letters by clicking on either the vowel button or consonant button.
An example being:
KEEIAQWL
The word LEAK is present.
:)
One way would be to check to individual letters as they are entered into the textbox by the user; if they don't exist don't add them - that way you will never need to check before your dictionary lookup.
A general way would involve looking at each character in the submitted word;
available_letters = "KEEIAQWL"
....
entered_word = "leak"
available_letters_temp = available_letters
entered_word = Ucase$(entered_word) '//ensure same case
dim i as long, pos as long
for i = 1 to Len(entered_word)
'// see if letter ok
pos = instr(1, available_letters_temp, mid$(entered_word, i, 1))
if pos = 0 then
msgboxMid$(entered_word, i, 1) & " is not valid"
else
'// ok, remove for future lookups
mid$(available_letters_temp, pos, 1) = "#"
end if
Next
This also makes sure that a letter can only be used once, i.e. "BOOB" from "BO" is not allowed.

How to make a char array in J2ME to work as a char in c++

The title is probably not accurate but I hope that reading this post you can understand what I want to do.
I'm kind stuck in here. New in Java ME, that unfortunately has, as you know, reduced methods than the Java SE.
What I want to accomplish is this: I have a txt, with numbers in it separated by space.
I want to put them in an array that can ""behave as an usual array in c++"" when one gets numbers separated by space into an array.
Now in J2ME what I've done (using Netbeans) is: I took the txt to a stream. Later sent the stream to a byte array and finally i converted it to a char array.
Let's say the original txt was: 98 2 12 13
Part of the code is:
InputStream is = getClass().getResourceAsStream("models.txt");
try{
int st_pk1_len = is.available();
byte st_pk1[] = new byte[st_pk1_len];
is.read(st_pk1);
char st_pk1_char[] = new String(st_pk1).toCharArray();
System.out.println(st_pk1_char);
What I get printed is: 98 2 12 13
Although, my problem is that when I want to access index 0 I get only the number 9 and not 98. If I try to reach the number 12, I put the pointer to 3 but what I get is an empty space and so on.
I've searched and tried different methods that I've found without luck to convert that into the original numbers again.
It could be a stupid mistake from my side or something that I've haven't think of.
Isn't there a simple solution to this problem?
update It's working now! Array is working as a "regular"c++ char array. In case somebody else need it or have my same problem, here is how it looks like:
InputStream is = getClass().getResourceAsStream("st_pk1.txt");
int st_pk1_len = is.available();
byte st_pk1[] = new byte[st_pk1_len];
is.read(st_pk1);
char st_pk1_char[] = new String(st_pk1).toCharArray();
String PreSplitted = new String(st_pk1);
String AftSplit[] = Split(PreSplitted, " ");
If you want to check: System.out.println(AftSplit[n]);
For the split method I used the second link in the Gnat's post.
Split text in J2ME
You can treat the char array as String, containing tokens separated by space: new String(st_pk1) does that for you.
After that, you need to split it, like as described in couple other Stack Overflow questions:
How do I split strings in J2ME?
Split text in J2ME

Resources