Read text file and separate text from numerical values

I have a text file that is output from an inspection machine, and looks like the attached file, just with a lot more holes. I am trying to create a matlab program that takes the values from this text file and puts them in to a template in excel and saved as a new excel file. The excel template has equations and plotting function that just needs the values placed in the empy fields, and is currently done by opening the text file in excel and doing the import, then copy and pasted over to the excel template. It works but is slow.
I want to take the values for each hole number, even though the only thing changing is the 'actual' values, and save them in an array so it's easier to place in excel. I can't find a good way to separate the row text from the numbers themselves, especially since there are multiple values on a row. Other than the first hole the data follows the same pattern of a description, diameter, x location, y location. The best I have so far is reading it in to a table where each row in the table is one row from the file. So it's a start but not a great one.
Maybe I'm overcomplicating the process too, but any help is appreciated.

 Accepted Answer

Depending on what exactly you want to import, this could work:
opts = fixedWidthImportOptions('NumVariables', 8, ...
'VariableNames', {'Feature', 'Unit', 'Nominal', 'Actual', 'Tolerance1', 'Tolerance2', 'Deviation', 'Exceeded'}, ...
'VariableWidths', [19, 6, 14, 14, 12, 11, 13, 2], ...
'VariableTypes', {'char', 'char', 'double', 'double', 'double', 'double', 'double', 'char'}, ...
'DataLines', [10 Inf], ...
'ImportErrorRule', 'omitrow', ...
'MissingRule', 'omitrow');
data = readtable('sample.txt', opts)
If you wanted to import the hole number or the step as well, you would have to parse the file instead. It probably would not be too complicated with judicious use of some regular expressions.

3 Comments

Thanks for the answer. That probably would work but I realized I have the 2015 version of MATLAB and am using it as a demo. I might try to update to the latest version though just for this to work. Thanks for your help.
readtable was a lot more basic in R2015a/b. It keeps improving with each version. In R2015a/b, you will have no choice but to write your own parser.
This fairly simple one would probably do what you want:
filecontent = fileread('Sample.txt'); %read whole file
data = regexp(filecontent, '^(Diameter|X Location|Y Location)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)', 'tokens', 'dotexceptnewline', 'lineanchors');
data = vertcat(data{:});
data(:, 3:7) = num2cell(str2double(data(:, 3:7))); %convert textual data to numeric for the numeric columns
data = cell2table(data, 'VariableNames', {'Feature', 'Unit', 'Nominal', 'Actual', 'Tolerance1', 'Tolerance2', 'Deviation', 'Exceeded'})
Wow. I get what it's doing but at the same time I don't. I will have to read more about the regexp function. Thank you so much for your help, I can definitely work with this.
Edit: Follow up question. I've been searching for an answer to this and you show up on a lot of answers. I have an excel template and the text data would be written to its own sheet, already in the template, and the other sheets have functions to analyze the data. This new populated excel file would be saved under a new name. Is there a way to do that? Everything I see is to just write to an existing file. I am pulling a part number out of the text file and using that as a string for a new excel file, so I was going to do a copyfile, but it doesn't seem to allow variables as the name.

Sign in to comment.

More Answers (0)

Asked:

on 14 Nov 2018

Edited:

on 15 Nov 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!