Resultant of two files

1 view (last 30 days)
Ahmad Nayan
Ahmad Nayan on 6 Jul 2021
Commented: Mathieu NOE on 8 Jul 2021
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 Comments
Ahmad Nayan
Ahmad Nayan on 8 Jul 2021
yes I need the operation for all section.
Mathieu NOE
Mathieu NOE on 8 Jul 2021
see my answer below ...

Sign in to comment.

Answers (1)

Mathieu NOE
Mathieu NOE on 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 Comments
Ahmad Nayan
Ahmad Nayan on 8 Jul 2021
I am using R2016a with student license from university
this error message
Attempt to execute SCRIPT strings as a function:
C:\Program Files\MATLAB\R2016a\toolbox\matlab\strfun\strings.m
Error in myfunction_read (line 6)
ll_out = strings; % init string array
Error in maincode (line 5)
[sections1,variables1,data1] = myfunction_read('1.txt');
Mathieu NOE
Mathieu NOE on 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

Sign in to comment.

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Tags

Products


Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!