Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
convert string of numbers to matrix

Subject: convert string of numbers to matrix

From: vladimir

Date: 24 Oct, 2010 00:19:04

Message: 1 of 8

Hello,
  I'm trying to read ANSY output file with tons of information and i need to filter everything. I was able to get all the information that i need into a bunch of strings. Now I need to convert those string into a matrix. Here is the example of one string that I have.
ans =
    ' 1 0.10340000E+09 2 0.00000000E+00 3 0.00000000E+00 4-0.10340000E+09'
    ' 5 0.00000000E+00 6 0.00000000E+00 7 0.00000000E+00 8 0.00000000E+00'
    ' 9 0.00000000E+00 10 0.00000000E+00 11 0.00000000E+00 12 0.00000000E+00'
    ' 13 0.00000000E+00 14 0.00000000E+00 15 0.00000000E+00 16 0.00000000E+00'
    ' 17 0.00000000E+00 18 0.00000000E+00'

From the top the number in front (1 through 18) of the decimal number is the position in the matrix, and the decimal number is the actual information that I need. I need to convert this into a matrix 1 by 18. Note that position 4 has a negative sign that needs to be kept. Thank you for your help and time.

Subject: convert string of numbers to matrix

From: Vladimir V Bogachuk

Date: 24 Oct, 2010 01:29:04

Message: 2 of 8


Also here is that the cell look like before i select ans=StiffAll(:,1).

StiffAll =

  Columns 1 through 6

    [1x80 char] [1x80 char] [1x80 char] [1x80 char] [1x80 char] [1x80 char]
    [1x80 char] [1x80 char] [1x80 char] [1x80 char] [1x80 char] [1x80 char]
    [1x80 char] [1x80 char] [1x80 char] [1x80 char] [1x80 char] [1x80 char]
    [1x80 char] [1x80 char] [1x80 char] [1x80 char] [1x80 char] [1x80 char]
    [1x40 char] [1x40 char] [1x40 char] [1x40 char] [1x40 char] [1x40 char]

then i run
ans=StiffAll(:,1)
ans =

    ' 1 0.10340000E+09 2 0.00000000E+00 3 0.00000000E+00 4-0.10340000E+09'
    ' 5 0.00000000E+00 6 0.00000000E+00 7 0.00000000E+00 8 0.00000000E+00'
    ' 9 0.00000000E+00 10 0.00000000E+00 11 0.00000000E+00 12 0.00000000E+00'
    ' 13 0.00000000E+00 14 0.00000000E+00 15 0.00000000E+00 16 0.00000000E+00'
    ' 17 0.00000000E+00 18 0.00000000E+00'


thank you for your time.

Subject: convert string of numbers to matrix

From: Vladimir V Bogachuk

Date: 24 Oct, 2010 01:32:06

Message: 3 of 8


Also here is that the cell look like before i select ans=StiffAll(:,1).

StiffAll =

  Columns 1 through 6

    [1x80 char] [1x80 char] [1x80 char] [1x80 char] [1x80 char] [1x80 char]
    [1x80 char] [1x80 char] [1x80 char] [1x80 char] [1x80 char] [1x80 char]
    [1x80 char] [1x80 char] [1x80 char] [1x80 char] [1x80 char] [1x80 char]
    [1x80 char] [1x80 char] [1x80 char] [1x80 char] [1x80 char] [1x80 char]
    [1x40 char] [1x40 char] [1x40 char] [1x40 char] [1x40 char] [1x40 char]

then i run
ans=StiffAll(:,1)
ans =

    ' 1 0.10340000E+09 2 0.00000000E+00 3 0.00000000E+00 4-0.10340000E+09'
    ' 5 0.00000000E+00 6 0.00000000E+00 7 0.00000000E+00 8 0.00000000E+00'
    ' 9 0.00000000E+00 10 0.00000000E+00 11 0.00000000E+00 12 0.00000000E+00'
    ' 13 0.00000000E+00 14 0.00000000E+00 15 0.00000000E+00 16 0.00000000E+00'
    ' 17 0.00000000E+00 18 0.00000000E+00'


thank you for your time.

Subject: convert string of numbers to matrix

From: james bejon

Date: 24 Oct, 2010 13:14:04

Message: 4 of 8

There's probably a cleverer way, but...

% DATA
A = {' 1 0.10340000E+09 2 0.00000000E+00 3 0.00000000E+00 4-0.10340000E+09';
     ' 5 0.00000000E+00 6 0.00000000E+00 7 0.00000000E+00 8 0.00000000E+00';
     ' 9 0.00000000E+00 10 0.00000000E+00 11 0.00000000E+00 12 0.00000000E+00';
     ' 13 0.00000000E+00 14 0.00000000E+00 15 0.00000000E+00 16 0.00000000E+00';
     ' 17 0.00000000E+00 18 0.00000000E+00'};

% ENGINE
[Junk, Tokens] = regexp(A, '\d+\w*([0-9\.\-]+E\+[0-9]+)\w*', 'tokens', 'match');
Res = str2double(regexprep([Tokens{:}], '\d\-', '-'))

Subject: convert string of numbers to matrix

From: james bejon

Date: 24 Oct, 2010 17:44:04

Message: 5 of 8

P.S. Slightly clearer is

str2double([regexp([A{:}], '([\w\-][\d\.]+E[\d\+]+)', 'tokens', 'match'){:}])

though I'm not sure if this uses Octave-only features. It's a better regular expression either way...

Subject: convert string of numbers to matrix

From: james bejon

Date: 24 Oct, 2010 20:56:03

Message: 6 of 8

P.S. I was thinking '\w' stood for whitespace in the above, but it doesn't. Using '\s' is more correct.

P.P.S. If you always have zeros before the decimal points, you can also use

% DATA
A = {' 1 0.10340000E+09 2 0.00000000E+00 3 0.00000000E+00 4-0.10340000E+09';
     ' 5 0.00000000E+00 6 0.00000000E+00 7 0.00000000E+00 8 0.00000000E+00';
     ' 9 0.00000000E+00 10 0.00000000E+00 11 0.00000000E+00 12 0.00000000E+00';
     ' 13 0.00000000E+00 14 0.00000000E+00 15 0.00000000E+00 16 0.00000000E+00';
     ' 17 0.00000000E+00 18 0.00000000E+00'};

% ENGINE
eval(strcat('[', regexprep([A{:}], '\s\d+', ' '), ']'))

Subject: convert string of numbers to matrix

From: Vladimir V Bogachuk

Date: 25 Oct, 2010 00:36:03

Message: 7 of 8

"james bejon" <jamesbejon@yahoo.co.uk> wrote in message <ia1bes$d8k$1@fred.mathworks.com>...
> There's probably a cleverer way, but...
>
> % DATA
> A = {' 1 0.10340000E+09 2 0.00000000E+00 3 0.00000000E+00 4-0.10340000E+09';
> ' 5 0.00000000E+00 6 0.00000000E+00 7 0.00000000E+00 8 0.00000000E+00';
> ' 9 0.00000000E+00 10 0.00000000E+00 11 0.00000000E+00 12 0.00000000E+00';
> ' 13 0.00000000E+00 14 0.00000000E+00 15 0.00000000E+00 16 0.00000000E+00';
> ' 17 0.00000000E+00 18 0.00000000E+00'};
>
> % ENGINE
> [Junk, Tokens] = regexp(A, '\d+\w*([0-9\.\-]+E\+[0-9]+)\w*', 'tokens', 'match');
> Res = str2double(regexprep([Tokens{:}], '\d\-', '-'))


Thank you for you time this worked perfectly

Subject: convert string of numbers to matrix

From: Vladimir V Bogachuk

Date: 25 Oct, 2010 08:38:04

Message: 8 of 8

Also can you help with this I have raw data that i need to filter here is an example I have this kind of data in text file.
%%%%%%%%%%
Stiffness matrix Harwell-Boeing format
           685 19 324 324 18
RSA 18 18 324 0
(I14) (I14) (d25.15) (d25.15)
F 1 18
             1
            19
            37
            55
            73
            91
           109
           127
           145
           163
           181
           199
           217
           235
           253
           271
           289
           307
           325
             1
             2
             3
             4
             5
             6
             7
             8
             9
            10
            11
            12
            13
            14
            15
            16
            17
            18
    0.103400000000000D+09
    0.000000000000000D+00
    0.000000000000000D+00
   -0.103400000000000D+09
    0.000000000000000D+00
    0.000000000000000D+00
    0.000000000000000D+00
    0.000000000000000D+00
    0.000000000000000D+00
    0.000000000000000D+00
    0.000000000000000D+00
    0.000000000000000D+00
    0.000000000000000D+00
    0.000000000000000D+00
    0.000000000000000D+00
    0.000000000000000D+00
    0.000000000000000D+00
    0.000000000000000D+00
    0.000000000000000D+00

First I need to filter this data so I can only have data that's after the the hole number (18) the thing is that this data will vary differently and i only need everything with decimals and the D should be replaced with E. Because the data that needed is E+9 or sometime it can me E-9. Thank you for your time

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us