Question

So basically, I have data in a text file like so:

100 5 10 20 someval someval
200 6 20 12 someval someval
300 7 30 13 someval someval

The first 3 tokens would by used as (x, y, z) coordinates, while the fourth number will be used to create a color to use the surf(x,y,x,c) function. I like to be able to store the other values in the line too.

Was it helpful?

Solution

Try using the TEXTSCAN function:

fid = fopen('file.txt','rt');
A = textscan(fid, '%f %f %f %d %s %s', 'Delimiter',' ');
fclose(fid);

XYZ = [A{1:3}]
clr = A{4}
valsStr = [A{5:6}]

the result:

XYZ =
   100     5    10
   200     6    20
   300     7    30
clr =
          20
          12
          13
valsStr = 
    'someval'    'someval'
    'someval'    'someval'
    'someval'    'someval'

where

>> whos XYZ clr valsStr
  Name         Size            Bytes  Class     Attributes

  XYZ          3x3                72  double              
  clr          3x1                12  int32               
  valsStr      3x2               444  cell                

OTHER TIPS

If you need to do this one time only, and you are using matlab with GUI, then you can just use File->import data , which is pretty smart about tabular formats.

But if you need to do this repeatedly, or make it part of your program, then you call the command line version importdata.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top