MATLAB Answers

How to take text files and turn the data into variables?

38 views (last 30 days)
Vincent Craven
Vincent Craven on 29 Oct 2015
Edited: Star Strider on 29 Oct 2015
When I try to use the automated function produced by the Import Tool it never works. I'm not quite sure what I'm doing wrong but it must be something. For example I am trying to take data in a text file ( organized in order across the top Time Angle Velocity Acceleration) and turn them into variables to be used in Matlab. I'm new to trying this and so any simple explanation could go a long way. Thank You

Accepted Answer

Star Strider
Star Strider on 29 Oct 2015
I would have to see your file to provide a definitive reply. My favourite function for text file reading is textscan. It might initially appear a bit complicated to use, but it’s actually straightforward. It has several options because it needs them to work and play well with different text files.
  14 Comments
Star Strider
Star Strider on 29 Oct 2015
The file name would change in the fopen statement each time. You could do that in a for loop. I would use textscan and if all the files have essentially the same filename but differed only in the number, something like this could work:
for k1 = 1:NumFiles
filnam = sprintf('LRNR%d.txt', k1);
fidi = fopen(filnam, 'r');
dc{k1} = textscan(fidi, '%f%f%f%f', 'Delimiter','\t', 'HeaderLines',7);
fclose(fidi);
end
This runs through the list of files, opens each, reads it, saves the data in the ‘dc’ cell array for each iteration of the loop, then closes the file. The data are all saved in ‘dc’.
When you’ve read all of them in, I would save ‘dc’ to a .mat file so you can just load the .mat file rather than having to read in the text files each time. See the documentation on the save function for details.
You can then extract each cell in ‘dc’ to a double array as I did with cell2mat (for example:
d = cell2mat(dc{1});
Time = d(:,1);
Angle = d(:,2);
Velocity = d(:,3)
Acceleration = d(:,4);
and the individual variable assignments to work on for each file.
The first section of code I entered here is UNTESTED but should work.

Sign in to comment.

More Answers (3)

Thorsten
Thorsten on 29 Oct 2015
The data file just contains comma separated values, you can use dlmread. For more complicated data, textscan is fine, as Star Strider recommends. For more specific advice, it please attach the data file.
  6 Comments
Vincent Craven
Vincent Craven on 29 Oct 2015
How do I make it so that it knows which files to use

Sign in to comment.


Vincent Craven
Vincent Craven on 29 Oct 2015
This is one of the files. I have about 15 that I need to use total. I don't know if it's possible to do all at once or if I need to do them individually. They all for the most part look just like this one.
  1 Comment
Vincent Craven
Vincent Craven on 29 Oct 2015
I clicked on the link and it comes out a little funny but for the most part is fine

Sign in to comment.


timo
timo on 29 Oct 2015
Might i suggest to use python CSV module from inside matlab ? Its stupidly powerfull
  1 Comment
Vincent Craven
Vincent Craven on 29 Oct 2015
No idea what that is or how to use it. If you'd like to teach I'd be up for trying

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!