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!