How to take text files and turn the data into variables?
11 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Star Strider
2015-10-29
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 个评论
Star Strider
2015-10-29
编辑:Star Strider
2015-10-29
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.
更多回答(3 个)
Thorsten
2015-10-29
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 个评论
Thorsten
2015-10-29
编辑:Thorsten
2015-10-29
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
2015-10-29
Might i suggest to use python CSV module from inside matlab ? Its stupidly powerfull
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Data Preparation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!