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:
txt2mat and E notation

Subject: txt2mat and E notation

From: Melanie Roberts

Date: 16 Sep, 2010 06:15:23

Message: 1 of 5

I am using txt2mat to read in a series of data from a txt file, however my data is all in exponent notation. For numbers with single digit exponents the read in works great (e.g. 1.54267E-9), however the moment I have two digits in the exponent, e.g. 3.6728475E-10, the exponent is ignored and it reads in as 3.6728475. Is it possible to have txt2mat recognise exponent form?

Thanks

Subject: txt2mat and E notation

From: Andres

Date: 16 Sep, 2010 19:40:23

Message: 2 of 5

"Melanie Roberts" <kthelephant@hotmail.com> wrote in message <i6sclr$9r5$1@fred.mathworks.com>...
> I am using txt2mat to read in a series of data from a txt file, however my data is all in exponent notation. For numbers with single digit exponents the read in works great (e.g. 1.54267E-9), however the moment I have two digits in the exponent, e.g. 3.6728475E-10, the exponent is ignored and it reads in as 3.6728475. Is it possible to have txt2mat recognise exponent form?

Hi Melanie,
that sounds strange. I have written a test file

"
Demo with different exponents
1 2 3e4
4e5 3.6728475E-10 1.54267E-9
1.54267E9 3.6728475E123 0E0
"

and with

A = txt2mat(fn);
num2str(A)

I get

ans =

         1 2 30000
    400000 3.6728475e-010 1.54267e-009
1542670000 3.6728475e+123 0

which is ok. Can you reproduce that?
It would be helpful if you post a small example file here. You may also contact me via the author page on the file exchange.

Regards
Andres

Subject: txt2mat and E notation

From: Melanie Roberts

Date: 17 Sep, 2010 02:16:06

Message: 3 of 5

Thanks Andres - that's given me the hint to fix the problem. That sample worked fine, and I found that wrapping things in a second declaration fixed the problem. Not sure if this should be the case or not. There are some oddities in the full code that could have been causing the problem perhaps - but have mostly ironed these out now.

Just in case you happen to be interested I've copied below the relevant part of the m-file and a short version of the data file that I am reading from (but without the odd lines)

Thanks again for your help

Melanie

% -----------------------------------------------------------------------

The relevant part of the m-file is

%
k = 15; %number of lines in a block
n = 2; %number of blocks
EfcnStart = 2;
EfcnEnd = 7;
ParsStart = 14;

data = txt2mat('short.txt',0); % call txt2mat with no headers

efcns = cell(n,1); % psi and w output values at each point in x.
pars = cell(n,1); % parameter values.

for i = 1:n
    dummy = data((EfcnStart+(i-1)*k):EfcnEnd+(i-1)*k,1:6); %
    efcns{i} =dummy;
    B1 = data(ParsStart+(i-1)*k,1:7);
    B2 = data(ParsStart+1+(i-1)*k,1);
    pars{i} = horzcat(B1,B2);
end

% creating a single parameter matrix
par = pars{1};
for i = 2:n
    par = vertcat(par,pars{i});
end

% ------------------------------------------------------------------------------

and a sample txt file is
     1 1 9 1 3 1 41 6 86 10 4 8 0 5 4 0
       0.0000000000E+00 1.0000000000E+00 -2.2978888872E-14 -4.9623658165E-07 -4.1149332330E-07 0.0000000000E+00
       2.5000000000E-02 6.9748385374E-01 -3.7459238716E-02 -7.4907958372E-01 3.4411914459E-03 2.5000000000E-02
       5.0000000000E-02 2.4005770200E-01 -5.0036231520E-02 -5.0160125984E-01 -2.4376890203E-02 5.0000000000E-02
       7.5000000000E-02 4.2323889607E-02 -1.8038063580E-02 -1.2124335607E-01 -1.6521181725E-02 7.5000000000E-02
       1.0000000000E-01 3.9764114969E-03 -2.4540997542E-03 -1.2507808895E-02 -3.2418181440E-03 1.0000000000E-01
       1.0000000000E+00 -1.6241261204-224 -1.6914064805-223 -4.5269271793-223 4.3468561590-224 1.0000000000E+00
    1 4 5
       1.8836890961E-01 -9.8209217224E-01 2.6822044812E-03
       0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 2.5569219096E-18
      -4.4275479484E-04 5.2727547886E-03 -1.0901030429E-03 -2.1506157196E-03 6.9313062219E-18
       3.3114340927E-08 -3.4422292743E-08 -1.2912822961E-08 -1.2301469494E-08 3.5553315421E-18
      -1.1074895985E-07 1.2382612602E-07 4.6265447556E-08 4.1379395110E-08 3.5553315421E-18
       1.0000000000E+00 1.0000000000E+00 1.0000000000E-01 2.6121900000E+00 -1.0038100000E-02 2.6764277136E+00 1.0000000000E+00
       1.0000000000E+00
     1 2 4 2 3 1 41 6 86 10 4 8 0 5 4 0
       0.0000000000E+00 1.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 -3.8319530394E-25
       2.5000000000E-02 6.8819973948E-01 -3.0152692663E-02 -7.0702468542E-01 4.5637544581E-03 2.5000000000E-02
       5.0000000000E-02 2.5558918194E-01 -4.7938011358E-02 -4.9531450013E-01 -2.1017584021E-02 5.0000000000E-02
       7.5000000000E-02 3.8555632579E-02 -2.3667497684E-02 -1.4506011523E-01 -2.1927454457E-02 7.5000000000E-02
       1.0000000000E-01 1.3102740304E-03 -1.2461276276E-03 -4.9158421907E-03 -4.1289612589E-04 1.0000000000E-01
       1.2500000000E-01 -1.8537376111E-05 2.3432375016E-05 2.7231404066E-05 4.5916308390E-05 1.2500000000E-01
    1 4 5
       1.8836890961E-01 -9.8209217224E-01 2.6822044812E-03
       0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 0.0000000000E+00 2.5569219096E-18
      -4.4275479484E-04 5.2727547886E-03 -1.0901030429E-03 -2.1506157196E-03 6.9313062219E-18
      -1.3609510020E-03 8.2637511192E-03 -1.6020287682E-03 2.7124433204E-03 -6.6180834016E-18
      -1.1074895985E-07 1.2382612602E-07 4.6265447556E-08 4.1379395110E-08 3.5553315421E-18
       1.0088129970E+00 1.0000000000E+00 1.0000000000E-01 2.6137758679E+00 -1.0952013730E-02 2.6764277136E+00 1.0000000000E+00
       1.0000000000E+00

Subject: txt2mat and E notation

From: Andres

Date: 17 Sep, 2010 11:00:21

Message: 4 of 5

"Melanie Roberts" <kthelephant@hotmail.com> wrote in message <i6uj16$57d$1@fred.mathworks.com>...
> [..]
> and a sample txt file is
> 1 1 9 1 3 1 41 6 86 10 4 8 0 5 4 0
> 0.0000000000E+00 1.0000000000E+00 -2.2978888872E-14 -4.9623658165E-07 -4.1149332330E-07 0.0000000000E+00
> 2.5000000000E-02 6.9748385374E-01 -3.7459238716E-02 -7.4907958372E-01 3.4411914459E-03 2.5000000000E-02
> 5.0000000000E-02 2.4005770200E-01 -5.0036231520E-02 -5.0160125984E-01 -2.4376890203E-02 5.0000000000E-02
> 7.5000000000E-02 4.2323889607E-02 -1.8038063580E-02 -1.2124335607E-01 -1.6521181725E-02 7.5000000000E-02
> 1.0000000000E-01 3.9764114969E-03 -2.4540997542E-03 -1.2507808895E-02 -3.2418181440E-03 1.0000000000E-01
> 1.0000000000E+00 -1.6241261204-224 -1.6914064805-223 -4.5269271793-223 4.3468561590-224 1.0000000000E+00
> [..]

Did you notice the errors in line 7 of your text file, "-1.6241261204-224" etc?

Subject: txt2mat and E notation

From: Andres

Date: 17 Sep, 2010 22:38:04

Message: 5 of 5

Obviously something went wrong while the text file was generated.
If you can be sure it is only the sporadically missing 'E', and you can't write clean files anew, you could use a regular expression to fix this:

data = txt2mat(filename, 0, 'ReplaceRegExpr', {{'(\d)([+-]\d)','$1E$2'}});

Good luck,
Andres

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