import text file using importdata
2 views (last 30 days)
Show older comments
Douwe Horsthuis
on 30 Aug 2022
Commented: Douwe Horsthuis
on 30 Aug 2022
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 Comments
Accepted Answer
Chunru
on 30 Aug 2022
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
More Answers (1)
Image Analyst
on 30 Aug 2022
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 Comments
Image Analyst
on 30 Aug 2022
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.
See Also
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!