join rows in a cell matrix with the row above

55 次查看(过去 30 天)
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

回答(1 个)

Voss
Voss 2024-6-21
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)
Col 1 Col 2 X777784 R22,R34,R536, D253,D273 X654646 D43,D263
% 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)
Col 1 Col 2 X777784 R22,R34,R536,D253,D273 X654646 D43,D263
  2 个评论
ethan
ethan 2024-6-24,10:24
Good Morning,
Thank you for the responce, i've been giving it a go but not managed to get this to work. It is a complex .txt that is being imported and i have simplified it down to a string array of the data that needs to be simplified. Screenshot of it as an example. Its the blank rows that need to be removed and if column one is empty merge entire row with the row above.
Any help is much appretiated.
Cheers
Voss
Voss 2024-6-24,15:15
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)
G38476 IC232_L,IC235 G821763 IC432_L,IC453 IC432_B,IB453 X777784 R22,R34,R536 D253,D273 D255,D275 X654646 D43,D263
% 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)
G38476 IC232_L,IC235 G821763 IC432_L,IC453,IC432_B,IB453 X777784 R22,R34,R536,D253,D273,D255,D275 X654646 D43,D263
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"]
str = 10x2 string array
"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)
"G38476" "IC232_L,IC235" "G821763" "IC432_L,IC453,IC432_B,IB453" "X777784" "R22,R34,R536,D253,D273,D255,D275" "X654646" "D43,D263"

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by