两个不够就自己加吧,逻辑一样的
% Define file paths
file1 = 'st-rfile.out39;;
% file2 = 'all_1_15611.out39;;
% Open the first file, read the header, and then read the data
fid1 = fopen(file1, 'rt39;);
if fid1 == -1, error('Cannot open file %s39;, file1); end
fgetl(fid1); % Skip the first line
fgetl(fid1); % Skip the second line
headerLine = fgetl(fid1); % Read the header line
headerLine = strrep(headerLine, '(39;, '39;); % Remove left parentheses
headerLine = strrep(headerLine, ')39;, '39;); % Remove right parentheses
headers = strsplit(headerLine, '34; "39;); % Split the header line into individual column names
headers = strrep(headers, '34;', 39;'); % Remove double quotes from headers
headers = headers(~cellfun('isempty39;, headers)); % Remove any empty cells
data1 = [];
while ~feof(fid1)
line = fgetl(fid1); % Read each line
if ~ischar(line), break; end % If it's not a character line, stop
nums = sscanf(line, '%f39;); % Parse the numbers
data1 = [data1; nums.'];
end
fclose(fid1);
% Open the second file and read the data
fid2 = fopen(file2, 'rt39;);
if fid2 == -1, error('Cannot open file %s39;, file2); end
fgetl(fid2); % Skip the title row
fgetl(fid2); % Skip the second row
fgetl(fid2); % Skip the header row as it is the same as the first file
data2 = [];
while ~feof(fid2)
line = fgetl(fid2); % Read each line
if ~ischar(line), break; end % If it's not a character line, stop
nums = sscanf(line, '%f39;); % Parse the numbers
data2 = [data2; nums.'];
end
fclose(fid2);
% Find the overlapping part in the second file with the first file
overlap_idx = find(data2(:,1) <= data1(end, 1), 1, 'last39;);
% Combine data
combined_data = [data1; data2(overlap_idx+1:end, :)]; % Skip the overlapping part
% Validate that the number of headers matches the number of data columns
if size(combined_data, 2) ~= length(headers)
error('The number of headers does not match the number of columns in the data.');
end
% Create a table with headers and combined data
combined_data_table = array2table(combined_data, 'VariableNames39;, headers);
% Extract the file name without extension from file1
[filepath, name, ~] = fileparts(file1);
% Construct the new file name with the extracted name and .csv extension
output_filename = fullfile(filepath, [name, '.csv39;]);
% Export the table to a CSV file with the new file name
writetable(combined_data_table, output_filename);
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删