% full path to files (I prefer working with full paths but you could just use filenames) clear all clc sample_folder= '/home/ali/Desktop/test_JSR'; s1 = fullfile(sample_folder, 'sample01.txt'); s2 = fullfile(sample_folder, 'sample02.txt'); % Read sample02 (this one's simple) s2Table = dlmread(s2,'',1,0); fid2 = fopen(s2); s2Headers = strsplit(fgetl(fid2)); s2Headers(end) = []; fclose(fid2); % Clean header strings to remove invalid variable name characters badChars = {'[', ']', '-', '#'}; % list all problematic characters in your headers here %s2Headers = erase(s2Headers, badChars); % Remove problematic chars; req. matlab 2016b s2Headers_new = s2Headers(find(s2Headers!=badChars)) s2Table.Properties.VariableNames = s2Headers; % Read sample01; it will be read in as a cell array of strings fid = fopen(s1); s1str = textscan(fid, '%s %s', 'HeaderLines', 2); fclose(fid); s1str = [s1str{:}]; % Now separate each sub-table into it's own table key = 'T[K]'; %the start of each row that identifies a new sub-table %headerIdx = contains(s1str(:,1), key); %logical index identifying header rows headerIdx = strcmp(s1str(:,1), "T[K]"); %logical index identifying header rows %so far it is Okey with Octave....Problem starts from now s1cell = splitapply(@(x){x}, s1str, cumsum(headerIdx)); s1table = cellfun(@(x)array2table(str2double(x(2:end, :))), s1cell, 'UniformOutput', false); % before combining data, tables need to have the same number of rows. % Here we pad the shorter table with trailing zeros. padRowsNeeded = size(s2Table,1) - cellfun(@(x)size(x,1), s1table); s1Table = cellfun(@(x,y)[x;array2table(zeros(y,size(x,2)))], s1table, num2cell(padRowsNeeded),'UniformOutput', false); % Now you can join columns to create a new array % Here is an example newMatrix = [s1Table{1}.Var1, s1Table{1}.Var2, s2Table.Var1, s2Table.Var2]; % Convert back to a table if you want to % Note that the variable names can be pulled directly from the source files but will required some cleaning % since some of them are invalid variable names. newTable = array2table(newMatrix, 'VariableNames', {'TK_samp01', 'NC7H16_samp1', 'TK_samp2', 'NC7H16_samp2'}) Undefined function or variable 's2Table'.