import text file using importdata
5 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
When I try to import text files with experiment infomation using importdata, it seems to only import a couple of lines from the .text file.
I use the following code that looks for the (attached) readme file
data_folder=dir(data_path); %data_path opens a participants folder with mulitple files including the readme file
for log=1:length(data_folder)
if endsWith( data_folder(log).name , 'README.txt' )
logfile_1 = importdata([data_path data_folder(log).name]);
end
end
Instead of loading the whole file I get the attached logfile_1 file. Where if you open the logfile_1.textdata we only have 3 rows and those are not complete. When googling solutions to loading .txt files this is I believe the way to go. I have tried loading it as a table, but that messes too much with the structure.
Does anyone have any idea why this happens and how I can force it to import the whole file?
Thank you for your time,
Douwe
0 个评论
采纳的回答
Chunru
2022-8-30
You data format is a special format. You can consider to read them as text and then extract individual information. Otherwise you have to use commands like fscanf to read and parse data line by line. importdata can not tackle this kind of data format.
s = fileread("1111 F.A.S.T. Response Task 0-00-0000 README.txt");
s
更多回答(1 个)
Image Analyst
2022-8-30
This is a custom file format that simple generic readers can't handle. You'll have to write your own reader function for this type of file.
allLines = readlines('1111 F.A.S.T. Response Task 0-00-0000 README.txt')
Then have a loop where you check for certain words in each line (might use contains for that) , and then parse the found line to extract the particular words or numbers you want.
3 个评论
Image Analyst
2022-8-30
OK, glad it works for you. But how did the answer you accepted instead actually parse out all the stuff for you? You said that "works like a dream" but, to me, it looks like it merely just sucks up the whole file into a single character array and not giving you things like Age.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 String Parsing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!