Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to transform a string to a categorical array in which the categories are the characters.
If you want to make a cell-array of strings (chars) then use the cellstr() function. This will turn each row of a char array into a separate string in a cell-array. Since your string variable is a single row, use the single-quote character to transpose it to a column and then use cellstr():
string ='abcd'
A = cellstr(string') % The single quote after the string variable transposes it to a column
The output A will be columnar, so to get a row cell-array stick another single quote after the A, for example in use with categorical() as you mention:
B = categorical(A')
You can use num2cell for this purpose as follows:
string ='abcd';
num2cell(string)
Output:-
ans =
'a' 'b' 'c' 'd'
strings in matlab are already really a vector of characters.
str = 'abcd';
length(str) %4
str(1) %a
str(2:3) %bc
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
I want to concatenate two strings into a single variable.
String datas1 = "demo1 String2" ;
String datas2 = "String1" ;
i want to get string be like:
String datas3 = "demo1String1 String2"
how to do that?
Don't miss to use " or ' to mention the start and end of String.
String datas1 = "ajnk muhammed";
Or
String datas1 = 'ajnk muhammed';
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have text as below,
TIME STAMP1
A1200 EVENT START
EVENT NAME = DOS
EVENT_INS = 1
EVENT_ID = 100
BUFFER = 233355
FORMAT = ATC
LOC = C:/User/data
;
TIME STAMP2
A1201 EVENT START
EVENT NAME = DOS
EVENT_INS = 0
EVENT_ID = 87
BUFFER = 773355
FORMAT = ETC
LOC = C:/User/data
;
how can I remove TIME STAMP2 based on A1201,need to remove from A1201 to ; using scala.A1201 sensor part will repeat at different location in the file. Wherever it comes, I need to remove from A1201 to ;..
How can I do with Scala Spark ?.
You can use the following simple solution
val rdd = sparkContext.wholeTextFiles("path to the text file")
rdd.map(x => x._2.replace("\n", "|*|").split(";").filter(!_.contains("A1201")).mkString(";").replace("|*|", "\n")+";")
where, wholeTextFiles would read the file in Tuple2 format with filename as first argument and text data as the second argument
x._2.replace("\n", "|*|") replaces the line feeds with special character which is to be used later
.split(";") splits the text at ; and forms array
.filter(!_.contains("A1201")) filters out all text from A1201 to ;
.mkString(";").replace("|*|", "\n")+";" converts the array of string to original format.
I hope the answer is helpful
This question already has answers here:
how to count unique elements of a cell in matlab?
(2 answers)
Closed 7 years ago.
I want to determine the number of times a character appears in a character array, excluding the time it appears at the last position.
How would I do this?
In Matlab computing environment, all variables are arrays, and strings are of type char (character arrays). So your Character Array is actually a string (Or in reality the other way around). Which means you can apply string methods on it to achieve your results. To find total count of occurrence of a character except on last place in a String/Character Array named yourStringVar you can do this:
YourSubString = yourStringVar(1:end-1)
//Now you have substring of main string in variable named YourSubString without the last character because you wanted to ignore it
numberOfOccurrence = length(find(YourSubString=='Character you want to count'))
It has been pointed out by Ray that length(find()) is not a good approach due to various reasons. Alternatively you could do:
numberOfOccurrence = nnz(YourSubString == 'Character you want to count')
numberOfOccurrence will give you your desired result.
What you can do is map each character into a unique integer ID, then determine the count of each character through histcounts. Use unique to complete the first step. The first output of unique will give you a list of all possible unique characters in your string. If you want to exclude the last time each character occurs in the string, just subtract 1 from the total count. Assuming S is your character array:
%// Get all unique characters and assign them to a unique ID
[unq,~,id] = unique(S);
%// Count up how many times we see each character and subtract by 1
counts = histcounts(id) - 1;
%// Show table of occurrences with characters
T = table(cellstr(unq(:)), counts.', 'VariableNames', {'Character', 'Counts'});
The last piece of code displays everything in a nice table. We ensure that the unique characters are placed as individual cells in a cell array.
Example:
>> S = 'ABCDABABCDEFFGACEG';
Running the above code, we get:
>> T
T =
Character Counts
_________ ______
'A' 3
'B' 2
'C' 2
'D' 1
'E' 1
'F' 1
'G' 1
This question already has answers here:
How can I concatenate strings in a cell array with spaces between them in MATLAB?
(5 answers)
Closed 9 years ago.
I have a variable which is the list of string of a listBox handle
string = get(handles.ListBox,'string');
string=
'file1'
'file2'
I want to create a single string like:
line= 'file1 file2'
In order to write it in one line in my output file. The number of input files may vary in every case so I would like to do it in a general way. I have tried 'horzcat', 'strcat' but no success.
There is a strjoin function specifically for this task:
C = {'one', 'two', 'three'};
str = strjoin(C)
The simplest way, and IMHO cleanest way, is to make a "matrix" out of them:
s1 = 'bla';
s2 = 'blabla';
scat = [ s1, s2 ];
I found a nice way:
S = {'file1'; 'file2'};
strjoin(S(:)', ' ')
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Using regexp to find a word
I'm working on an assignment for my CS course.
We're given a plain text file, which, in my case, contains a series of tweets.
What I need to do is create a script that will detect hashtags, and then save each hashtag into an cell array.
So far I know how to write a function that detects the '#' symbol...
strfind(textRead{i},'#');
where in a for loop where i=1:30 (that is, the number of cells of text). However, past that, I'm at a loss as to how I should write a script that will detect the '#' and return the text between that and the next ' ' (space) character.
Try this:
str = '#someHashtag other tweet text ignore #random';
regexp(str, '#[A-z]*', 'match')
I think you'll be able to find the rest out yourself :)
Here is basic skeleton. But make sure to use correct regexp to extract the values ;-)
Yes with the above Dorin's regexp and match you get one value at a time. You may add a token as per this example from mathworks.
Sample:
str = ['if <code>A </code> == x<sup>2 </sup>, ' ... '<em>disp(x) </em>']
str = if <code>A </code> == x<sup>2 </sup>, <em>disp(x) </em>
expr = '<(\w+).*?>.*?</\1>';
[tok mat] = regexp(str, expr, 'tokens', 'match');
tok{:}
ans = 'code'
ans = 'sup'
ans = 'em'
in above code you don't really need to loop and can process entire text bulk as one string , hopefully not hitting any string limit......
But if you want to loop, or if you need to loop, you use the following sample with Rody's regexp and match only.
fid = fopen('data.txt');
dataText = fgetl(fid);
while ~feof(fid)
ldata = textscan(dataText,'*%d#*');
X = (ldata, '#[A-z]*', 'match')
Cellarray = X{1}
end
Disp(X)
fclose(fid);