MATLAB: Code To Extract Data From .txt File to Excel

extract dataMATLABtext file

Hi,
I have several text files with two columns and usually 2000+ rows (picture below). I would like to have some code to extract the value from the bottom row of the second column and put it into an Excel file in a certain place. Each of the text files has a different heading/name (same format as picture below though) and I would like to put the value from the text file into Excel under these headings. If necessary, I can create the headings myself and would be happy to just have code which outputs the same heading's value in the same column each time.
Currently I am doing this manually which takes a long time and will need doing lots more in the future.
Does anyone know how to do this and could help me? Happy to answer more questions or give more details.
Many thanks

Best Answer

As we don't yet know how to identify the files to be processed, nor how exactly the data is to be exported to excel, the below just focuses on the file import:
filelist = {'b1-moment-z-rfile.txt', 'b2-moment-z-rfile.txt', 'b3-moment-z-rfile.txt'}; %it's not clear yet how this is to be obtained
datatoexport = cell(size(filelist)); %container for data to export as this is also not clear
for filenum = 1:numel(filelist)
wholecontent = readtable(filelist{filenum}); %read whole file, readtable automatically detects formatting and header and uses the header to name the table variables
datatoexport{filenum} = wholecontent(end, :); %keep last row of table (header always come with it)
end
With the above, you will get a warning for each file that matlab modifies the header to make it valid variable names (it removes the ( and " and replaces the - by _). The warning can be turned off with
warning('off', 'MATLAB:table:ModifiedAndSavedVarnames');
or matlab can be told not to do the replacement (thus keeping the (" in the variable names) with the 'PreserveVariableNames', false option of readtable.
Once details of how to identify files and what export is desired, the above can be modified accordingly.