×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Reading in the leading zeros in a .dat file

Reading in the leading zeros in a .dat file

Reading in the leading zeros in a .dat file

(OP)
Hi everyone,

I am trying to read a parsed xml file in the form of a .dat file into matlab. I have been using load filename.dat to obtain my matrix in the variable filename, but whenever I go to use the matrix, the leading zeros are missing from the first column.

The dat file has the following (shortened) contents:

0110    8    267    1    287
0111    10    266    3    286

Is there anyway that it can read it exactly how it is in the .dat file including these leading 0's?

Any help would be much appreciated.

Many thanks,

J

RE: Reading in the leading zeros in a .dat file

You would have to read the file with fscanf and read the numbers in as strings

M

--
Dr Michael F Platten

RE: Reading in the leading zeros in a .dat file

(OP)
Thanks for the speedy response.

How exactly do I read them in as strings? what is the format command?

I have:
fid = fread(filename);
A = fscanf(fid, format);

where I can't seem to find the correct format...

Thanks,

J

RE: Reading in the leading zeros in a .dat file

Have a look in the Matlab help browser for more information on the format string. the Matlab fscanf function is very similar to the C function of the same name. Have a look in a C manual for more info.

M

--
Dr Michael F Platten

RE: Reading in the leading zeros in a .dat file

Hi,

AFAIK, you can't store integers with leading zeros in a matrix.
 
You can try something like this :

M=dlmread('sample.dat');
sprintf('%04d\n',M(:,1))

Jérôme

RE: Reading in the leading zeros in a .dat file

(OP)
Many thanks for the help guys,

M=dlmread('sample.dat');
sprintf('%04d\n',M(:,1))

allowed me to get my data in the form i needed, but I have found another way around it so it's formatted when I am checking the values.

J

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources