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

New to MATLAB?

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

Asked by Mahan Soltani

Mahan Soltani (view profile)

on 24 Dec 2012

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

0 Comments

Mahan Soltani

Mahan Soltani (view profile)

Products

1 Answer

Answer by per isakson

per isakson (view profile)

on 24 Dec 2012
Edited by per isakson

per isakson (view profile)

on 26 Dec 2012
Accepted answer

See Delimited or formatted I/O to text files

.

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 Comments

Walter Roberson

Walter Roberson (view profile)

on 26 Dec 2012

I have edited the link syntax to fix the links.

per isakson

per isakson (view profile)

on 26 Dec 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 (view profile)

on 26 Dec 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.

per isakson

per isakson (view profile)

Contact us