1

In Matlab, let's say that I have the following string:

mystring = 'sdfkdsgoeskjgk elkr jtk34s ;3k54352642 643l j3kf p35j535';

And I want to extract all the digits in it to a vector such that each digit is standing by its own, so the output should be like:

output = [3 4 3 5 4 3 5 2 6 4 2....]

I tried to do it using this code and regex:

mystring = 'sdfkdsgoeskjgk elkr jtk34s ;3k54352642 643l j3kf p35j535';
digits = regexp(mystring, '[0-9]');
disp(digits);

But it gives me some weird 4-combined digits instead of what I need.

Madno
  • 910
  • 2
  • 12
  • 27

2 Answers2

4

By default, the output of regexp is in the index of the first character in each match which is why the numbers aren't the same as the digits in your string. You'll want to use the output of regexp to then index into the initial string to get the digits themselves

digits = mystring(regexp(mystring, '[0-9]'));

You will still need to convert these from characters to numbers so you can subtract off '0' to do this conversion

digits = mystring(regexp(mystring, '[0-9]')) - '0';

Alternately, you could specify the 'match' input to regexp to return the actual matching string itself. This will return a cell array which we can then convert to an array of numbers using str2double

digits = str2double(regexp(mystring, '[0-9]', 'match'))
Suever
  • 64,497
  • 14
  • 82
  • 101
1

I use transposing instead of any other existing function to convert a string into an array.

mystring = 'sdfkdsgoeskjgk elkr jtk34s ;3k54352642 643l j3kf p35j535';
digits   = regexp(mystring, '[0-9]');

array    = double(mystring(digits)')'-48;    % array of doubles
disp(array);
kdrtkl
  • 111
  • 1
  • 2
  • 9
  • `'` is [not the transpose](http://stackoverflow.com/questions/25150027/using-transpose-versus-ctranspose-in-matlab). Also, the transposes are completely unnecessary. – Suever Mar 26 '17 at 13:12
  • @Suever Doesn't `A'` give transpose of `A` in this case as has been asked in the question? I have just given a solution which had worked for me some time ago, and this solution would also work for the question of asker. Yet, you have my +1 for giving a better solution than me. – kdrtkl Mar 26 '17 at 14:40
  • It's best to always use `.'` because that will work regardless of the input format so that it yields a reliable transpose. The transpose trick I think you're trying to use here would be if you used `str2num`: `str2num(mystring(digits).')` – Suever Mar 26 '17 at 14:42
  • @Suever I had tried my own solution in MATLAB before I posted, it worked fine, so that's why I have shared. There is always a better solution. Thanks for sharing extra information. Best regards. – kdrtkl Mar 26 '17 at 14:46
  • Right but you don't need the transpose at all: `mystring(digits) - 48` – Suever Mar 26 '17 at 14:48
  • @Suever I have just noticed that `'` was truly unnecessary in this case since output is already an array. But when there is a string like `s='1234'` and if we would like to split these integers into an array such as `[1,2,3,4]`, I am using this transposing trick as `n = (str2num(s'))'`. That is why I used transposing in my asnwer. When it comes to strings, it converts it to double as `1234`. – kdrtkl Mar 26 '17 at 15:04