MATLAB Answers

fileDatastore of files with different extensions

20 views (last 30 days)
GS76
GS76 on 12 Jun 2019
Edited: GS76 on 14 Jun 2019
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 Comments

Sign in to comment.

Accepted Answer

Nimit Dhulekar
Nimit Dhulekar on 12 Jun 2019
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.

  1 Comment

GS76
GS76 on 13 Jun 2019
Thank you so much for your reply in this regard.
The second "myCustomReader" worked great. Is it possible run this routine with multiple files at once?
I am getting the following error when I run the first 2 lines. Please see the screen shot below. Could you perhaps assist? Thank you again.

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Translated by