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

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

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

Like, you know how if you click on the import data button at the top you can choose the option to do import data as a function or a script. The function option never works and the script does but I'm not sure how to integrate the script into a larger function. Does that make sense.
When I try to use it as a function it only takes the first column of the text document and puts it as variable ans. Not the variables Time, Angle, Velocity, and Acceleration
The ‘Import Data’ wizard uses textscan. I prefer to write my own textscan calls, so I use it only very rarely.
It is sometimes necessary to use fseek and a while (or for) loop with textscan if there are breaks in the file or if you want to read different formats for different parts of the file. That’s the reason we need to see you file (or a relevant part of it if it is large) to provide you with definitive guidance.
Try this:
fidi = fopen('LRNR1.txt','r');
dc = textscan(fidi, '%f%f%f%f', 'Delimiter','\t', 'HeaderLines',7, 'CollectOutput',1);
d = cell2mat(dc);
fclose(fidi);
I get an (80x4) double array for ‘d’. That imports correctly. (I checked the first 10 rows.)
the one posted below works as well. With this one is there any way to make it 4 (80x1) vectors for each variable involved
Yes. Delete the 'CollectOutput' argument, then assign each column to a different variable:
dc = textscan(fidi, '%f%f%f%f', 'Delimiter','\t', 'HeaderLines',7);
d = cell2mat(dc);
Time = d(:,1);
Angle = d(:,2);
Velocity = d(:,3)
Acceleration = d(:,4);
You both gave a different way to solve my problem. Shows how versatile this program is.
If I wanted to use what you said in order to gather the data from multiple text documents all at once and categorize them as Time1 Angle1 ... Time2 Angle2... and so forth would that be possible and how.
Do the ‘multiple documents’ have all the same format or each a different format? If they all have the same number of header lines and the same essential format, my textscan call and code and Thorsten’s should work. If the formats change between files, the textscan calls to each might have to change as well.
BTW, the Python introduction page is: Getting Started with Python. I haven’t used it, but that will tell you something about it.
All the documents are data pulled from the same detector for the same experiment. They're all 4 columns with a 7 line header. Just What would I type for it to know to use multiple documents. How would I get it to do it for multiple documents.
Like, isn't what you put for me to use specific to the LRNR1.txt file. How could I get it to work for 12 documents with different names but the same formats.
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)

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

Posted the document as another comment below.
data = textscan(fopen('LRNR1.txt'), '%f%f%f%f', 'headerlines', 7);
data = cell2mat(data);
is there some way to make it into four separate variables. Like Time1 Angle1 and so forth
And can I automate it to do it for all of the documents or does it need to be done one by one
Do read into four separate variables:
[Time, Angle, Velocity, Accleration] = textread('LRNR1.txt', '%f%f%f%f', 'headerlines', 7);
You can loop over all your files, if they always have 4 columns and 7 headerlines, i.e., are of the same format. Use
d = dir('LNRN*.txt');
for i = 1:numel(d)
[Time(:,i), Angle(:,i), Velocity(:,i), Accleration(:,i)] = textread(d(i).name, '%f%f%f%f', 'headerlines', 7);
end
This works if all the files have the same number of data points. Else use
[Time{i}, Angle{i}, Velocity{i}, Accleration{i}] = ...
How do I make it so that it knows which files to use

Sign in to comment.

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

I clicked on the link and it comes out a little funny but for the most part is fine

Sign in to comment.

Might i suggest to use python CSV module from inside matlab ? Its stupidly powerfull

1 Comment

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.

Categories

Community Treasure Hunt

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

Start Hunting!