Breaking up a string and then converting to a vector? - arrays

If I have three strings, such as 'Y20194', '219Y42', and '12345' how do I break these up into a vector like [Y 2 0 1 9 4], [1 2 3 4 5], and [2 1 9 Y 4 2]? I am using str2num, but I think I am missing a step (separating the individual numbers in the strings first) before I convert to numerical values. Also, the characters aren't reading correctly and using str2num gives me [].
I have a file with lines of strings such as the one above. I used fgetl to read each line of my file into strings but am kind of stuck beyond that.

You cannot have both characters and numbers in a numerical vector.
You can do the following:
s = 'Y20194';
c = cellstr(s')';
v = str2double(c);
Cell array c will have all the characters from s separated in to individual cells. Notice that you have to transpose the string s first.
In vector v the first value will be NaN since it's a character.

The char will be kept. and the numbers will be converted to double type.
If the input is not from reading a file, the code is as follows. The result1 is the cell containing the array you want:
If the input is one file, let's take this file as example: demo1.txt, which content as follows:
the codes to convert each line to what you want as follows. the code converts each line into what you want and then display it.
If you want to replace the 'Y' or other alphabets with zero, then the code will be as follows

Maybe STRSPLIT will help.
ts = strsplit('Y20194');
% ts <- {'Y', '2', '0', '1', '9', '4'}
And now you can try to convert each element in the vector individually to a number using str2num.
N = size(ts, 1);
str = cell(1, N);
for i=1:N;
str{i} = str2num(ts{i, 1});
end
But since some of the characters in the string aren't numbers (e.g., 'Y'), I wouldn't expect this to work perfectly.
(Its been a while, some of my indexes may be switched.)

If you want to change the Y into 0 a very simple solution is available:
str = 'Y20194';
str(str==Y)='0';
str - '0'

Related

Converting a string of chars to an array of numbers according to a dictionary in matlab

I have a string containing letters. I want to assing a number to each letter, and convert it to an array. Let's say I have
'ABAABCCBA'
and I have a dictionary such that A=1, B=2, C=3 and thus the array I want is
[1,2,1,1,2,3,3,2,1]
This is super easy to do in python, but I have to use matlab for some subsequent analysis. Any ideas as to how I can do this without switch case, as succinctly as possible? (Note that this is an MRE and the original string contains 20 different letters)
A general method, which doesn't assume that keys or values are consecutive, is as follows:
keys = 'ABC';
values = [1 200 30];
data = 'ABAABCCBA';
[~, result] = ismember(data, keys);
result = values(result);

Automatically assign number to each string in array in Matlab

I have a large cell array in Matlab (imported from Excel) containing numbers and strings.
Let's say the string part looks like this, just bigger with many columns and lines:
Table{1,1} = 'string A'
Table{2,1} = 'string B'
Table{3,1} = 'string B'
And the number part looks like this just bigger:
Table{1,2} = 5;
Table{2,2} = 10;
Table{3,2} = 15;
I am aware that there are disadvantages of working with arrays (right?), so I consider converting EVERYTHING to a numeric matrix by replacing the strings with numbers. (Possibly as a data set with headings - if you don't advise against that?)
My problem is that I have A LOT of different string entries, and I want to automatically assign a number to each entry, e.g. 1 for 'string A', 2 for 'string B' etc., such that:
Matrix(1,1) = 1
Matrix(2,1) = 2
Matrix(3,1) = 2
etc.
and for the numbers simply:
Matrix(1,2) = Table{1,2};
Matrix(2,2) = Table{2,2};
Matrix(3,2) = Table{3,2};
For the strings, I cannot assign the numbers by individual code for each string, because there are so many different string entries. Is there a way to "automate" it?
I am aware of this help site, https://ch.mathworks.com/help/matlab/matlab_prog/converting-from-string-to-numeric.html, but haven't found anything else helpful.
How would you do it?
Find the indices of both numbers and character entries in your cell array using isnumeric (or ischar) with cellfun. Then use third output argument of unique (or findgroups which requires R2015b) for assigning numbers to character entries of your cell array. Now just put the numbers into your required matrix as shown below:
tmp = cellfun(#isnumeric,Table); %Indices of Numbers
Matrix = zeros(size(Table)); %Initialising the matrix
[~, ~, ic] = unique(Table(~tmp)); %Assigning numbers to characters
Matrix(~tmp) = ic; %Putting numbers for characters
%Above two lines can be replaced with Matrix(~tmp) = findgroups(Table(~tmp)); in R2015b
Matrix(tmp) = [Table{tmp}]; %Putting numbers as they are

Matlab join array of strings

In ruby and other languages, I can create an array, push an arbitrary number of strings and then join the array:
ary=[]
...
ary.push some_str
ary.push some_other_str
...
result = ary.join ""
How do I accomplish this in matlab?
User story: my plot legend is composed of a variable number of strings. The number of strings is determined runtime, so I want to declare the array, add strings dynamically and then join the array to the legend string in the end of the script.
In MATLAB, String joining happens like the following
a = 'ding';
b = 'dong';
c = [a ' ' b]; % Produces 'ding dong'
P.S. a typeof(c,'char') shows TRUE in MATLAB because it "joins" all characters into C.
Suppose you want to start with an empty char placeholder. You can do this.
a = ``; % Produces an empty character with 0x0 size.
Then you can keep adding to the end of it; like this:
a = [a 'newly added'] % produces a = "newly added"
To prove that it works, do this again:
a = [a ' appended more to the end.'] % produces a = "newly added appended more to the end."
You can always use the end keyword that points to the last index of an array, but in this case you need to append to end+X where X is the extra number of characters you are appending (annoyingly). I suggest you just use the [] operator to join/append.
There is also this strjoin(C, delim) function which joins a cell C of strings using a delim delimiter (could be whitespace or whatever). But cheap and dirty one is the one I showed above.

How to substring of a string in matlab array

I have a matlab cell array of size 20x1 elements. And all the elements are string like 'a12345.567'.
I want to substitute part of the string (start to 9th index) of all the cells.
so that the element in matrix will be like 'a12345.3'.
How can I do that?
You can use cellfun:
M = { 'a12345.567'; 'b12345.567' }; %// you have 20 entries like these
MM = cellfun( #(x) [x(1:7),'3'], M, 'uni', 0 )
Resulting with
ans =
a12345.3
b12345.3
For a more advanced string replacement functionality in Matlab, you might want to explore strrep, and regexprep.
Another method that you can use is regexprep. Use regular expressions and find the positions of those numbers that appear after the . character, and replace them with whatever you wish. In this case:
M = { 'a12345.567'; 'b12345.567' }; %// you have 20 entries like these - Taken from Shai
MM = regexprep(M, '\d+$', '3');
MM =
'a12345.3'
'b12345.3'
Regular expressions is a framework that finds substrings within a larger string that match a particular pattern. In our case, \d is the regular expression for a single digit (0-9). The + character means that we want to find at least one or more digits chained together. Finally the $ character means that this pattern should appear at the end of the string. In other words, we want to find a pattern in each string such that there is a number that appears at the end of the string. regexprep will find these patterns if they exist, and replace them with whatever string you want. In this case, we chose 3 as per your example.

Combining array into one string (matlab)

I have something like the following:
A = [1 2 5; 1 5 7];
B = A(1,:);
I output B:
B = A(1,:);
B =
1 2 5
I am looking to combine what is contained in B into one single string:
1/2/5
You can use sprintf:
sprintf('%d/',B)
This will give you almost what you want, it will have unnecessary / in the end.
>> sprintf('%d/',B)
ans =
1/2/5/
If you want to remove it:
st = sprintf('%d/',B);
st(end) = [];
As #hmuster points out correctly, it is possible to do it with \b , the backspace character.
st = [sprintf('%d/',B) sprintf('\b')];
However, as #AndrewJanke points out correctly, it could become a problem if this string is written into a pipe or a file. So use it with caution.
If you want it done properly (IE reusable), there are two steps:
Convert your numbers to strings (this will allow later crazy values to be converted properly with num2str http://www.mathworks.com/help/matlab/ref/num2str.html
Concatenate your strings horizontally (you can use MATLAB concatenation property A = [B C]), but the functional way is strcat http://www.mathworks.com/help/matlab/ref/strcat.html

Resources