How to take text files and turn the data into variables?
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (3)
Thorsten
am 29 Okt. 2015
0 Stimmen
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 Kommentare
Vincent Craven
am 29 Okt. 2015
Thorsten
am 29 Okt. 2015
data = textscan(fopen('LRNR1.txt'), '%f%f%f%f', 'headerlines', 7);
data = cell2mat(data);
Vincent Craven
am 29 Okt. 2015
Vincent Craven
am 29 Okt. 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}] = ...
Vincent Craven
am 29 Okt. 2015
timo
am 29 Okt. 2015
0 Stimmen
Might i suggest to use python CSV module from inside matlab ? Its stupidly powerfull
1 Kommentar
Vincent Craven
am 29 Okt. 2015
Kategorien
Mehr zu Text Data Preparation finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!