MATLAB: Reading content of a text file by readtable

؟

Hello
I have this file exported by another software so I cannot make an Excel of it. I used readtable command to read numeric content below different variables. But it reads the second variables like BSTEN, BPR, BDENO AND BDENG but not the variables of the first row like FOE. How can I get all the columns of it?
I appreciate it in advance
fid=fopen('FAR.txt','w');
copyfile FAR.RSM FAR.txt
fclose(fid);
fid = fopen('FAR.txt');
opts = detectImportOptions('FAR.txt');
T=readtable('FAR.txt',opts);
T.FOE(3:end,1)

Best Answer

Given the long conversation after the first rudimentary "throwaway" solution, I'll post a more general and robust solution as separate answer...
function t=readRunFar(file)
% return table containing data from input file
fid=fopen(file,'r');
% read the fullfile as cell array, convert to string array, ignore the FORTRAN formfeed
c=textscan(fid,'%s','delimiter','\n','Whitespace','','CommentStyle','1','CollectOutput',0);
fid=fclose(fid);
c=string(c{:});
ix=find(contains(c,"SUMMARY"))-1; % get section starting lines
N=numel(ix); % the number of sections in file
iyr=find(contains(c,"YEARS")); % the start of second column of data
iyr=strfind(c(iyr(1)),"YEARS")-1;
c(ix(2):end)=extractAfter(c(ix(2):end),iyr); % remove the TIME column after first set
iend=find(char(c(end))~=' ',1,'last'); % find last data on last section
d=c(1:ix(2)-1); % begin a new data array with first section
for i=2:N-1 % join subsequent sections to first
d=strcat(d, c(ix(i):ix(i+1)-1));
end
d=strcat(d, extractBefore(c(ix(N):end),iend+1));
d=d(4:end); % trash the beginning header lines
d=d(~all(char(d)==' '|char(d)=='-' ,2));% and the other extraneous text
data=str2num(char(d(4:end))); % convert to numeric
vnames=split(d(1)); % get the variable names from header
vnames=vnames(~(vnames=="")); % split() leaves empties...
vnames=categorical(vnames,unique(vnames,'stable')); % create ordered categorical variable
cats=categories(vnames); % the category names for logical addressing
t=table(data(:,1),'VariableNames',cats(1)); % initial table entry
for i=2:numel(cats) % and build the output table
t=[t table(data(:,vnames==cats(i)),'VariableNames',cats(i))];
end
end
The above reads the whole file into memory and mungs on it to rearrange it into one section by catenating the subsequent sections to the right of the first; snipping the TIME column from subsequent sections. It then parses the header line and creates a table--I ended up using a categorical variable and its facility to be used as logical addressing look to find associated columns instead of the alternative binning technique--that actually worked quite nicely! :)
This returns each duplicated column into the table with the generic name as an array of M columns; it is left as "Exercise for Student" to parse the units line and the node IDs to go with the variables.
I also attached the m file to be a little more convenient given its size