Delimit a vector by decimal

Hi,
After importing my data and delimiting by white space, I am left with two columns. For example, I am left with a matrix with a column of
1.2340 or similar numbers like 122.433
and a column of
233 or similar numbers
My problem is that the left column has to be split at the decimal into two separate numbers--but exactly how it appears. If the number on the right hand side of the decimal ends in a 0, I need to preserve it, as this isn't actually a decimal, but two identifying numbers stuck together with a decimal. Every solution I have tried thus far that has separated the numbers have dropped the 0. Any suggestions?

14 comentarios

Azzi Abdelmalek
Azzi Abdelmalek el 22 de Oct. de 2013
Not clear
Mason
Mason el 22 de Oct. de 2013
Ok, to clarify, just assume that I have a column of numbers such as
1.233
22.330
540.3001
What I need to do here is separate this column into two separate columns; one containing the digits on the left of the decimal, and the other containing the digits on the right of the decimal.
My problem so far has been keeping the 0 in the second entry; everything I have tried has shortened 330 to 33.
Azzi Abdelmalek
Azzi Abdelmalek el 22 de Oct. de 2013
Have you imported those data as a char or as double?
Mason
Mason el 22 de Oct. de 2013
double
Azzi Abdelmalek
Azzi Abdelmalek el 22 de Oct. de 2013
Ok, but why one number is with 3 decimals and another with 4 decimals? It's not possible
Azzi Abdelmalek
Azzi Abdelmalek el 22 de Oct. de 2013
Editada: Azzi Abdelmalek el 22 de Oct. de 2013
Check this
A=[1.233;22.330;540.3001]
The result
A =
1.2330
22.3300
540.3001
Azzi Abdelmalek
Azzi Abdelmalek el 22 de Oct. de 2013
It's better if you post a sample of your txt file
Mason
Mason el 22 de Oct. de 2013
I'm not sure why, but when I import the data that I am working with, it imports it fine.
The only thing I'm having a problem with is separating them.
Azzi Abdelmalek
Azzi Abdelmalek el 22 de Oct. de 2013
What is the difference between 1.30 and 1.300?
dpb
dpb el 22 de Oct. de 2013
Editada: dpb el 22 de Oct. de 2013
But you'll play h, e, double-hockey sticks separating them as doubles.
Import as cell array of strings then split on the character decimal, then convert those substrings to numeric. That way you'll keep the '330' example as the full substring to convert instead of just the numeric remainder.
Or, of course, fix the broken data encoding scheme in the generating process and export two columns of properly scaled values instead of the combination.
Mason
Mason el 22 de Oct. de 2013
Here is a sample:
3001.1 0.082086927956593
3001.2 0.000152538100647958
3001.3 4.77296878570862E-05
3001.11 0.000174921404962367
3001.12 0.000147929354099336
3001.65 0.0254316164029534
3001.120 0.947370074332119
3001.121 0.0215693042587116
3001.122 0.00378786288621699
3001.126 0.382907657356411
3001.132 0.00166090942239532
3001.133 0.175693456921313
the numbers on the right are irrelevant; they get their own column when I import the data and delimit by white space. They are actually decimal values.
The numbers on the left, for example
3001.1
are the ones I need to separate.
This becomes a problem at the entry
3001.120
When I try to separate this column by the decimal, it will drop the 0, and the 120 will become 12.
Azzi Abdelmalek
Azzi Abdelmalek el 22 de Oct. de 2013
If you import 1.3 and 1.300 as double, the result is the same
Mason
Mason el 22 de Oct. de 2013
I'm not sure how I was successfully getting the different numbers, then...but do you have a way around this?
Mason
Mason el 22 de Oct. de 2013
@dpb, the broken data encoding scheme is actually that of an outside company...so frustrating! Haha
But, how would I import a DAT file as a cell array? I'm not familiar with the process, though it is probably simple?

Iniciar sesión para comentar.

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 22 de Oct. de 2013

1 voto

If you import your data as cell array of char
A={'1.233';'22.330';'540.3001'}
out=regexp(A,'\.','split')
out=str2double(reshape([out{:}],2,[])')

3 comentarios

Mason
Mason el 22 de Oct. de 2013
This definitely works when I manually input A.
But how do I get A to be an imported DAT file?
Azzi Abdelmalek
Azzi Abdelmalek el 22 de Oct. de 2013
fid=fopen('file.dat')
M=textscan(fid,'%s %s')
fclose(fid)
A=M{1}
Mason
Mason el 22 de Oct. de 2013
Thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 21 de Oct. de 2013

Comentada:

el 22 de Oct. de 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by