How to take text files and turn the data into variables?
12 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Vincent Craven
il 29 Ott 2015
Modificato: Star Strider
il 29 Ott 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
0 Commenti
Risposta accettata
Star Strider
il 29 Ott 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 Commenti
Star Strider
il 29 Ott 2015
Modificato: Star Strider
il 29 Ott 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.
Più risposte (3)
Thorsten
il 29 Ott 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 Commenti
Thorsten
il 29 Ott 2015
Modificato: Thorsten
il 29 Ott 2015
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}] = ...
timo
il 29 Ott 2015
Might i suggest to use python CSV module from inside matlab ? Its stupidly powerfull
Vedere anche
Categorie
Scopri di più su Text Data Preparation in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!