Code for file processing

Please, can someone help me inspect the Matlab code (below) to see what I need to correct? Thank you.
Examples of a verse:
  1. Mat 6:27
  2. Luk 2:52
Example of a version:
  1. NIV
  2. AMP
Example of a sentence:
  1. Col 2:10 (AMP) And in Him you have been made complete [achieving spiritual stature through Christ], and He is the head over all rule and authority [of every angelic and earthly power].
  2. Luk 2:52 (NIV) And Jesus grew in wisdom and stature, and in favor with God and man.
There are repetition of verses in the file, but in some cases, there are no repetitions.
For example, Mat 6:27 occurs two times, but Col 2:10 occurs only once.
Pseudo code (The Matlab code is below):
  1. Open the text file “stature.txt”
  2. Create a new file in MATLAB and name it “processed_stature.txt”
  3. Find a sentence that has the first bible verse and “NIV” in the file “stature.txt” and write that sentence in the file “processed_stature.txt”
  4. If there is no sentence that has the first bible verse and “NIV”, then find a sentence that has the first bible verse and “NKJV” and write that sentence in the file “processed_stature.txt”
  5. If there is no sentence that has the first bible verse and “NKJV”, then write the sentence of the first bible version that has the bible verse in the file “processed_stature.txt”
  6. Write the first other bible version that has the first bible verse in a square bracket.
  7. Move to the second bible verse in the file “stature.txt”
  8. Find a sentence that has the second bible verse and “NIV” and write that sentence in the file “processed_stature.txt”
  9. If there is no sentence that has the second bible verse and “NIV”, then find a sentence that has the second bible verse and “NKJV” and write that sentence in the file “processed_stature.txt”
  10. If there is no sentence that has the second bible verse and “NKJV”, then write the sentence of the first bible version that has the bible in the file “processed_stature.txt”
  11. Write first other bible version that has the first bible verse in a square bracket
  12. Move to the third bible verse in the file “stature.txt”
  13. Find a sentence that has the third bible verse and “NIV” and write that sentence in the file “processed_stature.txt”
  14. If there is no sentence that has the third bible verse and “NIV”, then find a sentence that has the third bible verse and “NKJV” and write that sentence in the file “processed_stature.txt”
  15. If there is no sentence that has the third bible verse and “NKJV”, then write the sentence of the first bible version that has the bible in the file “processed_stature.txt”
  16. Write first other bible version that has the first bible verse in a square bracket
  17. There should be no repetition of a verse in a sentence. So, a verse should be inspected once in the file "stature.txt"
  18. Continue this iteration till the end of the file “stature.txt”
  19. Open processed_stature.txt
% Open the input file "stature.txt"
inputFile = fopen('stature.txt', 'r');
if inputFile == -1
error('Error opening "stature.txt"');
end
% Create the output file "processed_stature.txt"
outputFile = fopen('processed_stature.txt', 'w');
if outputFile == -1
fclose(inputFile);
error('Error creating "processed_stature.txt"');
end
% Read the lines from the input file
lines = textscan(inputFile, '%s', 'delimiter', '\n');
lines = lines{1};
fclose(inputFile);
% Initialize variables for verse and versions
% I don't want to input the verses. I want the code to get the verses from the input file.
verses = {'Col 2:10', 'Luk 2:52', 'Mat 6:27', 'Luk 19:3', 'Luk 12:25', 'Eph 4:13'};
% I don't want to be inputting the versionPriority.
% I want the versionPriority to be generated in the code.
% The version priority should be 'NIV', 'NKJV', then the first version that
% contain the verse
versionPriority = {'NIV', 'NKJV', 'AMP'};
% Loop through the verses
for i = 1:numel(verses)
verse = verses{i};
foundSentence = '';
remainingVersions = '';
% Check each line for the current verse
for j = 1:numel(lines)
line = lines{j};
% Check if the line contains the current verse
if contains(line, verse)
% Check for the preferred version
for k = 1:numel(versionPriority)
version = versionPriority{k};
versionPattern = ['\(', version, '\)'];
if contains(line, versionPattern)
foundSentence = line;
versionIndex = strfind(line, versionPattern);
line(versionIndex:versionIndex+numel(versionPattern)-1) = '';
remainingVersions = [remainingVersions, '[', line, '] '];
break;
end
end
if ~isempty(foundSentence)
break;
end
end
end
% If the preferred version is not found, choose the first available version
if isempty(foundSentence)
for j = 1:numel(lines)
line = lines{j};
if contains(line, verse)
foundSentence = line;
remainingVersions = [remainingVersions, '[', line, '] '];
break;
end
end
end
% Write the processed sentence to the output file
fprintf(outputFile, '%s %s\n', foundSentence, remainingVersions);
end
fclose(outputFile);
disp('Processed "stature.txt". Output saved in "processed_stature.txt".');
stature.txt
Luk 2:52 (AMP) And Jesus kept increasing in wisdom and in stature, and in favor with God and men. [1Sa 2:26]
Luk 19:3 (AMP) Zaccheus was trying to see who Jesus was, but he could not see because of the crowd, for he was short in stature.
Col 2:10 (AMP) And in Him you have been made complete [achieving spiritual stature through Christ], and He is the head over all rule and authority [of every angelic and earthly power].
Luk 2:52 (NIV) And Jesus grew in wisdom and stature, and in favor with God and man.
Mat 6:27 (NKJV) Which of you by worrying can add one cubit to his stature?
Luk 2:52 (NKJV) And Jesus increased in wisdom and stature, and in favor with God and men.
Luk 12:25 (NKJV) And which of you by worrying can add one cubit to his stature?
Luk 19:3 (NKJV) And he sought to see who Jesus was, but could not because of the crowd, for he was of short stature.
Eph 4:13 (NKJV) till we all come to the unity of the faith and of the knowledge of the Son of God, to a perfect man, to the measure of the stature of the fullness of Christ;
Processed_stature.txt (the result the Matlab code is generating)
Col 2:10 (AMP) And in Him you have been made complete [achieving spiritual stature through Christ], and He is the head over all rule and authority [of every angelic and earthly power]. [Col 2:10 (AMP) And in Him you have been made complete [achieving spiritual stature through Christ], and He is the head over all rule and authority [of every angelic and earthly power].]
Luk 2:52 (AMP) And Jesus kept increasing in wisdom and in stature, and in favor with God and men. [1Sa 2:26] [Luk 2:52 (AMP) And Jesus kept increasing in wisdom and in stature, and in favor with God and men. [1Sa 2:26]]
Mat 6:27 (NKJV) Which of you by worrying can add one cubit to his stature? [Mat 6:27 (NKJV) Which of you by worrying can add one cubit to his stature?]
Luk 12:25 (NKJV) And which of you by worrying can add one cubit to his stature? [Luk 12:25 (NKJV) And which of you by worrying can add one cubit to his stature?]
Eph 4:13 (NKJV) till we all come to the unity of the faith and of the knowledge of the Son of God, to a perfect man, to the measure of the stature of the fullness of Christ; [Eph 4:13 (NKJV) till we all come to the unity of the faith and of the knowledge of the Son of God, to a perfect man, to the measure of the stature of the fullness of Christ;]
Expected processed_stature.txt (this is the result I want the code to generate)
Luk 2:52  (NIV)  And Jesus grew in wisdom and stature, and in favor with God and man.
[NKJV, AMP]
Luk 19:3  (NKJV)  And he sought to see who Jesus was, but could not because of the crowd, for he was of short stature.
[AMP]
Col 2:10  (AMP)  And in Him you have been made complete [achieving spiritual stature through Christ], and He is the head over all rule and authority [of every angelic and earthly power].
[]
Mat 6:27  (NKJV)  Which of you by worrying can add one cubit to his stature?
[]
Luk 12:25  (NKJV)  And which of you by worrying can add one cubit to his stature?  
[]
Eph 4:13  (NKJV)  till we all come to the unity of the faith and of the knowledge of the Son of God, to a perfect man, to the measure of the stature of the fullness of Christ;
[]

 Accepted Answer

Voss
Voss on 1 Aug 2023
Edited: Voss on 1 Aug 2023
Here's one way to do it, with intermediate output along the way so you can follow along.
% read the file
str = readlines('stature.txt');
% split each line into three strings: verse name, version, content
C = regexp(str,'(.*?)\((.*?)\)(.*)','tokens','once');
% concatenate to a string array, remove leading and trailing whitespace
% (strtrim) of each string:
str = strtrim(vertcat(C{:}))
str = 9×3 string array
"Luk 2:52" "AMP" "And Jesus kept increasing in wisdom and in stature, and in …" "Luk 19:3" "AMP" "Zaccheus was trying to see who Jesus was, but he could not …" "Col 2:10" "AMP" "And in Him you have been made complete [achieving spiritual…" "Luk 2:52" "NIV" "And Jesus grew in wisdom and stature, and in favor with God…" "Mat 6:27" "NKJV" "Which of you by worrying can add one cubit to his stature?" "Luk 2:52" "NKJV" "And Jesus increased in wisdom and stature, and in favor wit…" "Luk 12:25" "NKJV" "And which of you by worrying can add one cubit to his statu…" "Luk 19:3" "NKJV" "And he sought to see who Jesus was, but could not because o…" "Eph 4:13" "NKJV" "till we all come to the unity of the faith and of the knowl…"
% get the unique set of verse names, in the order they appear in the file,
% and the index in the unique set of the verse on each line in the file:
[verses,~,jj] = unique(str(:,1),'stable')
verses = 6×1 string array
"Luk 2:52" "Luk 19:3" "Col 2:10" "Mat 6:27" "Luk 12:25" "Eph 4:13"
jj = 9×1
1 2 3 1 4 1 5 2 6
% note: this generates the complete set of verse names that are in the file, in order:
verses(jj)
ans = 9×1 string array
"Luk 2:52" "Luk 19:3" "Col 2:10" "Luk 2:52" "Mat 6:27" "Luk 2:52" "Luk 12:25" "Luk 19:3" "Eph 4:13"
% set the version priority: "NIV" and "NKJV", followed by the rest from the file:
versionPriority = ["NIV"; "NKJV"];
versionPriority = [versionPriority; unique(str(~ismember(str(:,2),versionPriority),2))]
versionPriority = 3×1 string array
"NIV" "NKJV" "AMP"
% open the output file:
fid = fopen('processed_stature.txt','w');
% loop over each unique verse:
for ii = 1:numel(verses)
% find the line numbers on which this verse appears:
idx = find(ii == jj);
% now reorder those line numbers according to versionPriority:
[v_ism,v_idx] = ismember(versionPriority,str(idx,2));
idx = idx(v_idx(v_ism));
% write the first (i.e., highest priority, now) verse name, version, content:
fprintf(fid,'%s (%s) %s\n',str(idx(1),:));
% write the other versions:
fprintf(fid,'[%s]\n',strjoin(str(idx(2:end),2),', '));
end
% close the output file:
fclose(fid);
% check the result:
type processed_stature.txt
Luk 2:52 (NIV) And Jesus grew in wisdom and stature, and in favor with God and man. [NKJV, AMP] Luk 19:3 (NKJV) And he sought to see who Jesus was, but could not because of the crowd, for he was of short stature. [AMP] Col 2:10 (AMP) And in Him you have been made complete [achieving spiritual stature through Christ], and He is the head over all rule and authority [of every angelic and earthly power]. [] Mat 6:27 (NKJV) Which of you by worrying can add one cubit to his stature? [] Luk 12:25 (NKJV) And which of you by worrying can add one cubit to his stature? [] Eph 4:13 (NKJV) till we all come to the unity of the faith and of the knowledge of the Son of God, to a perfect man, to the measure of the stature of the fullness of Christ; []

3 Comments

Thank you so much
Here's another way, more like what you had already attempted.
% Open the input file "stature.txt"
inputFile = fopen('stature.txt', 'r');
if inputFile == -1
error('Error opening "stature.txt"');
end
% Create the output file "processed_stature.txt"
outputFile = fopen('processed_stature.txt', 'w');
if outputFile == -1
fclose(inputFile);
error('Error creating "processed_stature.txt"');
end
% Read the lines from the input file
lines = textscan(inputFile, '%s', 'delimiter', '\n');
lines = lines{1};
fclose(inputFile);
% Initialize variables for verse and versions
% I don't want to input the verses. I want the code to get the verses from the input file.
verses = {'Col 2:10', 'Luk 2:52', 'Mat 6:27', 'Luk 19:3', 'Luk 12:25', 'Eph 4:13'};
% I don't want to be inputting the versionPriority.
% I want the versionPriority to be generated in the code.
% The version priority should be 'NIV', 'NKJV', then the first version that
% contain the verse
versionPriority = {'NIV', 'NKJV', 'AMP'};
n_versions = numel(versionPriority);
% Loop through the verses
for i = 1:numel(verses)
verse = verses{i};
foundSentence = '';
remainingVersions = '';
has_version = false(1,n_versions);
version_line = cell(1,n_versions);
% Check each line for the current verse
for j = 1:numel(lines)
line = lines{j};
% Check if the line contains the current verse
if contains(line, verse)
% Check for ALL versions
for k = 1:numel(versionPriority)
version = versionPriority{k};
versionPattern = ['(', version, ')'];
if contains(line, versionPattern)
has_version(k) = true;
version_line{k} = line;
end
end
end
end
if any(has_version)
% write the first version's entire line
idx = find(has_version,1);
fprintf(outputFile, '%s\n', version_line{idx});
% write the other version names
has_version(idx) = false;
fprintf(outputFile, '[%s]\n', strjoin(versionPriority(has_version),', '));
end
end
fclose(outputFile);
disp('Processed "stature.txt". Output saved in "processed_stature.txt".');
Processed "stature.txt". Output saved in "processed_stature.txt".
type processed_stature.txt
Col 2:10 (AMP) And in Him you have been made complete [achieving spiritual stature through Christ], and He is the head over all rule and authority [of every angelic and earthly power]. [] Luk 2:52 (NIV) And Jesus grew in wisdom and stature, and in favor with God and man. [NKJV, AMP] Mat 6:27 (NKJV) Which of you by worrying can add one cubit to his stature? [] Luk 19:3 (NKJV) And he sought to see who Jesus was, but could not because of the crowd, for he was of short stature. [AMP] Luk 12:25 (NKJV) And which of you by worrying can add one cubit to his stature? [] Eph 4:13 (NKJV) till we all come to the unity of the faith and of the knowledge of the Son of God, to a perfect man, to the measure of the stature of the fullness of Christ; []
This way may be about as close as I can get to your initial approach:
% Open the input file "stature.txt"
inputFile = fopen('stature.txt', 'r');
if inputFile == -1
error('Error opening "stature.txt"');
end
% Create the output file "processed_stature.txt"
outputFile = fopen('processed_stature.txt', 'w');
if outputFile == -1
fclose(inputFile);
error('Error creating "processed_stature.txt"');
end
% Read the lines from the input file
lines = textscan(inputFile, '%s', 'delimiter', '\n');
lines = lines{1};
fclose(inputFile);
% Initialize variables for verse and versions
% I don't want to input the verses. I want the code to get the verses from the input file.
verses = {'Col 2:10', 'Luk 2:52', 'Mat 6:27', 'Luk 19:3', 'Luk 12:25', 'Eph 4:13'};
% I don't want to be inputting the versionPriority.
% I want the versionPriority to be generated in the code.
% The version priority should be 'NIV', 'NKJV', then the first version that
% contain the verse
versionPriority = {'NIV', 'NKJV', 'AMP'};
% Loop through the verses
for i = 1:numel(verses)
verse = verses{i};
foundSentence = '';
remainingVersions = '';
% line number where the highest priority version of this verse is found
foundOnLine = [];
% Check each line for the current verse
for j = 1:numel(lines)
line = lines{j};
% Check if the line contains the current verse
if contains(line, verse)
% Check for the preferred version
for k = 1:numel(versionPriority)
version = versionPriority{k};
versionPattern = ['(', version, ')'];
if contains(line, versionPattern)
foundSentence = line;
foundOnLine = j;
break;
end
end
if ~isempty(foundSentence)
break;
end
end
end
% check all lines, except the one where the highest priority version was
% found, for other versions
linesToCheck = 1:numel(lines);
if ~isempty(foundOnLine)
linesToCheck(foundOnLine) = [];
end
for j = linesToCheck
line = lines{j};
if contains(line, verse)
for k = 1:numel(versionPriority)
version = versionPriority{k};
versionPattern = ['(', version, ')'];
if contains(line, versionPattern)
versionIndex = strfind(line, versionPattern);
remainingVersions = [remainingVersions ', ' line(versionIndex+1:versionIndex+numel(versionPattern)-2)];
end
end
end
end
if ~isempty(remainingVersions)
% remove the initial ', '
remainingVersions([1 2]) = '';
end
% Write the processed sentence to the output file
fprintf(outputFile, '%s\n', foundSentence);
fprintf(outputFile, '[%s]\n', remainingVersions);
end
fclose(outputFile);
disp('Processed "stature.txt". Output saved in "processed_stature.txt".');
Processed "stature.txt". Output saved in "processed_stature.txt".
type processed_stature.txt
Col 2:10 (AMP) And in Him you have been made complete [achieving spiritual stature through Christ], and He is the head over all rule and authority [of every angelic and earthly power]. [] Luk 2:52 (AMP) And Jesus kept increasing in wisdom and in stature, and in favor with God and men. [1Sa 2:26] [NIV, NKJV] Mat 6:27 (NKJV) Which of you by worrying can add one cubit to his stature? [] Luk 19:3 (AMP) Zaccheus was trying to see who Jesus was, but he could not see because of the crowd, for he was short in stature. [NKJV] Luk 12:25 (NKJV) And which of you by worrying can add one cubit to his stature? [] Eph 4:13 (NKJV) till we all come to the unity of the faith and of the knowledge of the Son of God, to a perfect man, to the measure of the stature of the fullness of Christ; []

Sign in to comment.

More Answers (1)

Instead of
lines = textscan(inputFile, '%s', 'delimiter', '\n');
you might like to use readlines
lines = readlines(inputFile);
to get all the lines separately in one variable.
Otherwise it looks like you're on your way to success. If you need help, see

1 Comment

Thank you. Please, can you or someone help me go through the code and point out what I am doing wrong?

Sign in to comment.

Categories

Find more on Historical Contests in Help Center and File Exchange

Products

Release

R2023a

Asked:

on 1 Aug 2023

Commented:

on 1 Aug 2023

Community Treasure Hunt

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

Start Hunting!