Resultant of two files
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi, I have two files in .txt format and want to get resultant value into 3rd file. the A data is start in Line34 and Col 18, B - Col 32, C - Col 46 and D Col 60.. Thank you
5 Kommentare
Antworten (1)
Mathieu NOE
am 7 Jul. 2021
hello again
in the mean time I have worked for you ... this is a code to load the data for all sections (variables)
you get the total amount of sections loaded, the variable names and the data .
You only have to add your code to select which rows are needed for future tasks
hope it helps
%%%%%%%% main code %%%%%%%%%
clc
clearvars
% read data from 1st file
[sections1,variables1,data1] = myfunction_read('1.txt');
% read data from 2nd file
[sections2,variables2,data2] = myfunction_read('1y.txt');
%%%%%%% function %%%%%%%%%
function [sections,variables_all,data_all_blocks] = myfunction_read(filename);
lines = readlines(filename,'WhitespaceRule','trim');
% init data
freq_line_index = Inf;
ll_out = strings; % init string array
variables_all = [];
data_one_block = [];
data_all_blocks = [];
sections = 0;
for ci = 1:numel(lines)
ll = lines(ci,:); % current line
if startsWith(ll,'FREQ') %
sections = sections+1;
freq_line_index = ci;
lls = split(ll);
data_string0 = [char(lls(1))];
data_string1 = [char(lls(2)) char(lls(3))];
data_string2 = [char(lls(4)) char(lls(5))];
data_string3 = [char(lls(6)) char(lls(7))];
variables = {data_string0,[data_string1 '_Amplitude'],[data_string1 '_Phase'],[data_string2 '_Amplitude'],[data_string2 '_Phase'],...
[data_string3 '_Amplitude'],[data_string3 '_Phase']};
variables_all = [variables_all variables];
data_all_blocks = [data_all_blocks data_one_block];
data_one_block = [];
end
if ci >= freq_line_index+3
lls = split(ll);
data_one_block = [data_one_block ; (str2num(char(lls)))'];
end
end
% last section iteration / concatenation
data_all_blocks = [data_all_blocks data_one_block];
end
5 Kommentare
Mathieu NOE
am 8 Jul. 2021
ok
we even don't need that line so I removed it
%%%%%%%% main code %%%%%%%%%
clc
clearvars
% read data from 1st file
[sections1,variables1,data1] = myfunction_read('1.txt');
% read data from 2nd file
[sections2,variables2,data2] = myfunction_read('1y.txt');
%%%%%%% functions %%%%%%%%%
function lines = my_readlines(filename)
% work around for earlier matlab releases (not having readlines)
lines = regexp(fileread(filename), '\r?\n', 'split');
if isempty(lines{end}); lines(end) = []; end %end of file correction
end
function [sections,variables_all,data_all_blocks] = myfunction_read(filename);
% lines = readlines(filename,'WhitespaceRule','trim');
lines = my_readlines(filename);
% init data
freq_line_index = Inf;
variables_all = [];
data_one_block = [];
data_all_blocks = [];
sections = 0;
for ci = 1:numel(lines)
% ll = lines(ci,:); % current line % to be used with "readlines"
ll = convertCharsToStrings(lines{ci}); % current line % to be used with "my_readlines"
if contains(ll,' FREQ ') % % to be used with "my_readlines"
sections = sections+1;
freq_line_index = ci;
lls = split(ll);
data_string0 = [char(lls(1+1))];
data_string1 = [char(lls(2+1)) char(lls(3+1))];
data_string2 = [char(lls(4+1)) char(lls(5+1))];
data_string3 = [char(lls(6+1)) char(lls(7+1))];
variables = {data_string0,[data_string1 '_Amplitude'],[data_string1 '_Phase'],[data_string2 '_Amplitude'],[data_string2 '_Phase'],...
[data_string3 '_Amplitude'],[data_string3 '_Phase']};
variables_all = [variables_all variables];
data_all_blocks = [data_all_blocks data_one_block];
data_one_block = [];
end
if ci >= freq_line_index+3
lls = split(ll);
data_one_block = [data_one_block ; (str2num(char(lls)))'];
end
end
% last section iteration / concatenation
data_all_blocks = [data_all_blocks data_one_block];
end
Siehe auch
Kategorien
Mehr zu Data Import and Analysis finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!