how to import a text (.txt) file to MATLAB?

how can i import a text (.txt) file that incloude several column and row whitout using Exel ??? i copy txt file to a new exel file and use : [numdata,txtdata]=xlsread('MyExelFile') to import it in MATLAB , and i want directly import this txt file to MATLAB without using Exel... Answer me...Thanks

 Akzeptierte Antwort

per isakson
per isakson am 24 Dez. 2012
Bearbeitet: per isakson am 26 Dez. 2012

1 Stimme

.
Interactively:
I think the simplest way is to read the file interactively with "Import Data ..." in the File menu (R2012a).
.
Width code:
fid = fopen('cssm.txt', 'r' );
cac = textscan( fid, '%f%f%f%f%f%f', 'CollectOutput', true, 'Headerlines', 2 );
fclose( fid );
cac{:}
returns
ans =
1.0e-03 *
0 0 0 0 0 0
0 0 0 0 0 0
1.0000 0 1.0000 0 1.0000 0
1.0000 0 1.0000 0 1.0000 0
1.0000 0 1.0000 0 1.0000 0
where cssm.txt contains
XY
COP x COP y COP z
0 0 0 0 0 0
0 0 0 0 0 0
0.001 0 0.001 0 0.001 0
0.001 0 0.001 0 0.001 0
0.001 0 0.001 0 0.001 0
.
Including the header:
fid = fopen('cssm.txt', 'r' );
h1 = textscan( fid, '%s', 1 );
h2 = textscan( fid, '%s%s%s%s%s%s', 1, 'CollectOutput', true );
cac = textscan( fid, '%f%f%f%f%f%f', 'CollectOutput', true, 'Headerlines', 0 );
fclose( fid );
h1{:}
h2{:}
cac{:}
returns
ans =
'XY'
ans =
'COP' 'x' 'COP' 'y' 'COP' 'z'
ans =
1.0e-03 *
0 0 0 0 0 0
0 0 0 0 0 0
1.0000 0 1.0000 0 1.0000 0
1.0000 0 1.0000 0 1.0000 0
1.0000 0 1.0000 0 1.0000 0
.
Mimicing xlsread output
fid = fopen('forceplate_3.txt', 'r' );
h1 = fgetl( fid );
h2 = textscan( fid, '%s%s%s%s%s%s', 1, 'CollectOutput', true );
cac = textscan( fid, '%f%f%f%f%f%f', 'CollectOutput', true, 'Headerlines', 0 );
fclose( fid );
txtdata = repmat({''}, 2, 6 );
txtdata(1,1)= {h1};
txtdata(2,:)= h2{:};
numdata = cac{:};
the results are:
>> whos *data
Name Size Bytes Class Attributes
numdata 21000x6 1008000 double
txtdata 2x6 1382 cell

12 Kommentare

Mahan Soltani
Mahan Soltani am 24 Dez. 2012
thanks per, but my English is bad , and i can't translate those helps... can you write here a code that import this text file to MATLAB???
Is this two rows of header?
XY
COP x COP y COP z
0 0 0 0 0 0
0 0 0 0 0 0
0.001 0 0.001 0 0.001 0
0.001 0 0.001 0 0.001 0
0.001 0 0.001 0 0.001 0
Mahan Soltani
Mahan Soltani am 24 Dez. 2012
yes ser...
per isakson
per isakson am 24 Dez. 2012
Do you want to read the header lines?
Mahan Soltani
Mahan Soltani am 24 Dez. 2012
no, i want read all columns and rows. a command similar : [numdata,txtdata]=xlsread('...') that shom num and txt data...
per isakson
per isakson am 24 Dez. 2012
Your turn! You rearrange the data as you see fit.
Mahan Soltani
Mahan Soltani am 24 Dez. 2012
thanks A Lot Per...
Mahan Soltani
Mahan Soltani am 24 Dez. 2012
Bearbeitet: Walter Roberson am 26 Dez. 2012
Sorry, how can i get this data??? (my exel and text files name is: forceplate_3) when i open exel file with this command: [numdata,txtdata]=xlsread('forceplate_3') . i see this files in workspace : 1- http://theme.tk/wp-content/uploads/2012/12/1.jpg and after click on numdata , i see: 2- http://theme.tk/wp-content/uploads/2012/12/3.jpg and click on txtdata,show: 3- http://theme.tk/wp-content/uploads/2012/12/2.jpg , i save this exe file as a text file with name: forceplate_3.txt , and i need get just this data from txt file(like xls file) , can you give me a m file??? my exel file and txt file is : Download
per isakson
per isakson am 26 Dez. 2012
Bearbeitet: per isakson am 26 Dez. 2012
The first three links all give me
The page you were looking for doesn't exist.
You may have mistyped the address or the page may have moved.
the fourth, Download, gives me a zip-file, which contains an excel-file and a text-file with identical data. These data agree with the data of cssm.txt above.
The code, which I proposed above, reads the text-file, forceplate_3.txt, without problems. However, it is more robust to read the first header line with
h1 = fgetl( fid );
This line will accept any text in the first row.
Walter Roberson
Walter Roberson am 26 Dez. 2012
I have edited the link syntax to fix the links.
per isakson
per isakson am 26 Dez. 2012
Bearbeitet: per isakson am 30 Dez. 2012
@Walter, thanks!
@Mohammad,
  • If you want to learn Matlab you need to experiment a bit. You should not assume us to do everything for you.
  • Next time you post a question please show that you made an effort yourself!
Walter Roberson
Walter Roberson am 26 Dez. 2012
Odd, those first two links do show images for me. The aren't much of interest though, just variable explorer showing the size of the two arrays.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Convert Image Type finden Sie in Hilfe-Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by