0

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.

Autonomous
  • 8,935
  • 1
  • 38
  • 77
Abi
  • 47
  • 3
  • 1
    converting a string to an array of scalar values (has been answered before): http://stackoverflow.com/questions/12255056/matlab-converting-the-string-321-to-3-2-1?lq=1 How do you want to handle the non-numeric values though, i.e. what will `Y` above be in the resulting array? – gevang Mar 13 '13 at 19:18
  • I have a file of strings and I wanted to read them into a matrix and change the Ys into 0s. I am using `fgetl` to read my file in, and then I'm stuck at how to read these strings into vectors since I was thinking that once I am able to transform these strings into vectors, I can use `find` to change the Ys to 0s like I'd like. – Abi Mar 13 '13 at 21:54
  • as in the above, linked answer you can go with something like `s = 'Y20194'; v = arrayfun(@str2double, s); v(isnan(v)) = 0;`, that will convert the non-numerical values to `0` (or any value). – gevang Mar 13 '13 at 23:25

4 Answers4

2

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.

yuk
  • 19,098
  • 13
  • 68
  • 99
1

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:

enter image description here

If the input is one file, let's take this file as example: demo1.txt, which content as follows: enter image description here

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.

enter image description here

If you want to replace the 'Y' or other alphabets with zero, then the code will be as follows

enter image description here

tqjustc
  • 3,624
  • 6
  • 27
  • 42
  • 1
    Ah yes, the second one is what I'm looking for. Thank you! What if after I have read it into a cell array, I want to convert the 'Y's into a numbers. For example, after I've read in the file of strings, I want to change all the Ys to 0s? Would I just use the `find` command to replace the Ys with 0s? I know that would work for a normal numeric vector but would it work for a cell array too? – Abi Mar 13 '13 at 21:48
  • 1
    Hi, I have updated the answer. See the last figure. If you want to 'array' instead of 'cell', you can change Line 11 to be 'result1=[]'. To use 'find' to locate one element in cell, you can refer to this post: http://stackoverflow.com/questions/8061344/how-to-search-for-a-string-in-cell-array-in-matlab – tqjustc Mar 14 '13 at 00:15
0

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.)

Cody A. Ray
  • 5,869
  • 1
  • 37
  • 31
  • I believe STRSPLIT only appears in the latest version 2013a. And you need additional arguments to split a string to individual characters. See the [documentation](http://www.mathworks.com/help/matlab/ref/strsplit.html) for an example. – yuk Mar 27 '13 at 15:58
0

If you want to change the Y into 0 a very simple solution is available:

str = 'Y20194';
str(str==Y)='0';
str - '0'
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122