fileDatastore of files with different extensions
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi all,
I have attached a live script file.
I am trying to import a number of text files that have an *.out file extension, into a datastore function. I have attached a few of this files in a zipped file.
I am trying to extract the numerical data under the "'COS(NT)" and "SIN(NT)" headings in each file.
I am using the datastore function as I thought this maybe better than trying to loop through the files after creating variables for the file. I am new to programming and Matlab, hence my struggles.
Any assistance and/or suggestions would be much appreciated.
Regards,
Gary
댓글 수: 0
채택된 답변
Nimit Dhulekar
2019년 6월 12일
Hi Gary,
I looked at the attached files and it appears that you are only interested in data in a handful of lines (4 to be exact) in a significantly large file. If the data happens to appear consistently on the same lines in the file, you could consider creating a fixed width import options object and setting the DataLines property. You can then pass this object to readtable in your custom reader function that you supplied to fileDatastore.
fds = fileDatastore("Out/", "ReadFcn", @myCustomReader, "FileExtensions", ".out");
read(fds)
function R = myCustomReader(filename)
opts = delimitedTextImportOptions("NumVariables", 6, "DataLines", ...
[70849:70850; 70910:70911], "VariableTypes", ...
["char", "char", "double", "double", "double", "double"], ...
"Delimiter", " ");
R = readtable(filename, opts);
R.Var1 = []; % Note that the initial space on the data line appears as varaibles and has to be manually removed
R.Var2 = [];
end
However, if your data is not expected to appear on the same line in every file, then you would have to use a more complicated approach. A code pattern similar to the following should work.
function R = myCustomReader(filename)
F = fileread(filename);
Fsplits = split(F, newline);
F_varArray = contains(Fsplits, "COS(NT)");
idx = find(F_varArray == 1);
row1 = Fsplits(idx(1)+2);
row2 = Fsplits(idx(1)+3);
row3 = Fsplits(idx(2)+2);
row4 = Fsplits(idx(2)+3);
splitRow1 = split(row1, " ");
splitRow1(1:2) = [];
val1 = str2double(splitRow1');
splitRow2 = split(row2, " ");
splitRow2(1:2) = [];
val2 = str2double(splitRow2');
splitRow3 = split(row3, " ");
splitRow3(1:2) = [];
val3 = str2double(splitRow3');
splitRow4 = split(row4, " ");
splitRow4(1:2) = [];
val4 = str2double(splitRow4');
R = table([val1; val2; val3; val4]);
end
Hope this helps.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Standard File Formats에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!