join rows in a cell matrix with the row above
13 views (last 30 days)
Show older comments
Hello,
I am trying to import different .txt files and when the value in column 1 =0 merge that row with the row above So the values in the cells are merged into the singular cell above. These text files will have a consitant format as seen below but the individual values will be different. So needing it to work for all .txt imported.
Example of the matrix:
Col 1 Col 2
X777784 R22,R34,R536,
D253,D273
X654646 D43,D263
Desired Output:
Col 1 Col 2
X777784 R22,R34,R536,D253,D273
X654646 D43,D263
0 Comments
Answers (1)
Voss
on 21 Jun 2024
Something like this may work on your files (if not, upload a sample file using the paperclip button):
file_in = 'input.txt';
file_out = 'output.txt';
% show the input file's content, for reference
type(file_in)
% read the input file into a string array
str = readlines(file_in);
% get the indices of the lines that start with a space
idx = find(startsWith(str," "));
% iterate backwards over those
for ii = idx(end:-1:1).'
% combine each with the previous line, and strtrim to remove the space
str(ii-1) = str(ii-1)+strtrim(str(ii));
% delete the now-redundant line
str(ii) = [];
end
% write the new string array to the output file
writelines(str,file_out)
% check the output file
type(file_out)
2 Comments
Voss
on 24 Jun 2024
Try the modified code below. It contains one new line that removes blank elements (i.e., those which contain only whitespace or are zero length) from the string array str.
Note that this approach (which is the same basic approach as in my original answer) operates on the file directly and produces a new file (which can then be more easily imported, one way or another); it does not operate on a string array derived in some unknown manner from the file's contents.
file_in = 'input.txt';
file_out = 'output.txt';
% show the input file's content, for reference
type(file_in)
% read the input file into a string array
str = readlines(file_in);
% remove lines that are all whitespace (including lines of zero length)
str(arrayfun(@(s)all(isspace(s)),str)) = [];
% get the indices of the lines that start with a space
idx = find(startsWith(str," "));
% iterate backwards over those
for ii = idx(end:-1:1).'
% if the previous line doesn't end with a comma
if ~endsWith(str(ii-1),",")
% append a comma to it
str(ii-1) = str(ii-1)+",";
end
% combine each with the previous line, and strtrim to remove the space
str(ii-1) = str(ii-1)+strtrim(str(ii));
% delete the now-redundant line
str(ii) = [];
end
% write the new string array to the output file
writelines(str,file_out)
% check the output file
type(file_out)
If you want to operate on an n-by-2 string array rather than a file, then
% an example n-by-2 string array
str = [ ...
"G38476","IC232_L,IC235"; ...
"",""; ...
"G821763","IC432_L,IC453"; ...
" ","IC432_B,IB453"; ...
"",""; ...
"X777784","R22,R34,R536"; ...
" ","D253,D273"; ...
" ","D255,D275"; ...
""," "; ...
"X654646","D43,D263"]
% replace any all-whitespace elements with ""
str(arrayfun(@(s)all(isspace(s)),str)) = "";
% remove rows of str that are all ""
str(all(strcmp(str,""),2),:) = [];
% get the indices of the rows of str whose first element is ""
idx = find(strcmp(str(:,1),""));
% iterate backwards over those
for ii = idx(end:-1:1).'
% if the element at that row in column 2 of str doesn't end with a comma
if ~endsWith(str(ii-1,2),",")
% append a comma to it
str(ii-1,2) = str(ii-1,2)+",";
end
% combine the column 2 element with the element in column 2 of the previous line
str(ii-1,2) = str(ii-1,2)+str(ii,2);
% delete the now-redundant line
str(ii,:) = [];
end
% check the new string array
disp(str)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!