MATLAB Answers

Tinkul
0

Import txt file into matlab using programically

Asked by Tinkul
on 11 Sep 2012
Accepted Answer by Jan
I have data in txt file as shown below:
ffffffffffffffffffffffffffff
ffffffffffffffffffffffffffff
01234efefabcdeffffffffffffff
ffffffffffffabcdeabcdeffffff
abcdefabcdefabcdefabcdefabcd
abcdffffffefefefefbabadededf
1234dace56778445dcfacedbebbf
abcdfedfedabce34127432ffedab
now these data i have to take in m file by importing txt file into matlab.For that what matlab code should be written.....
Thanks

  4 Comments

Show 1 older comment
It is still not clear what form the data is in. Is the data a continuous string of digits, like you've indicated, or is it separated into numbers by spaces, line breaks, commas, etc.? Can you post the first few lines of the file into your question? Also, can you tell us what your desired output for those lines looks like?
Jan
on 12 Sep 2012
And what is the desired output? A String or are these characters 4 Byte hex-encoded signed integers?
It is continuous string of digits.There is no commas,spaces etc.Output should be same as in the txt file.

Sign in to comment.

3 Answers

Answer by Jan
on 12 Sep 2012
Edited by Jan
on 12 Sep 2012
 Accepted Answer

fid = fopen(FileName, 'r');
if fid == -1, error('Cannot open file'); end
S = fread(fid, Inf, '*char');
fclose(fid);
Or to use a Matlab function:
S = fileread(FileName);

  0 Comments

Sign in to comment.


Answer by Kevin Claytor on 11 Sep 2012

The Documentation has rather extensive help for file opening, reading and saving http://www.mathworks.com/help/techdoc/ref/f16-5702.html#f16-14492. I will direct you to a few in particular;

  0 Comments

Sign in to comment.


Answer by K
on 11 Sep 2012

I've used the command textread really successfully with .txt files where the columns of numbers were separated by spaces or tabs. The command looked something like this:
textread('Nameoftextfile.txt','%f %f %f','headerlines',12);
Try searching for textread in the Matlab help to get details for your specific application.

  0 Comments

Sign in to comment.