how to combine text files using load function?

i want to combine A,B,C files into one file.
But i faced error , Error using horzcat Dimensions of arrays being concatenated are not consistent.
this is my code
%put files to combine
File = {file_name_1, file_name_2,file_name_3};
A = [];
for index = 1 : numOfFile
newA = load(File{index});
A = [A newA];
end
% save final output
save('outputFile.txt', 'A')

댓글 수: 4

Kevin Chng
Kevin Chng 2018년 10월 4일
편집: Kevin Chng 2018년 10월 4일
I hope this help you
combinedata = [];
for K = 1 : 42
thisfilename = sprintf('file_name_%1d.txt', K);
thisdata = load(thisfilename); %load just this file
combinedata = [combinedata;thisdata]
end
%save final output
save('outputFile.txt','combinedata')
I modified the code from this link
Kevin Chng's solution puts the three files below each other, and presumes that the three files have the same number of columns.
The original code tries to put the three files beside each other, and presumes that the three files have the same number of rows.
If the three files must go beside each other but do not have the same number of rows, then you cannot create a numeric array from them unless you pad the shorter entries.
combinedata = [combinedata;thisdata];
this line happens same error.
Kevin Chng
Kevin Chng 2018년 10월 4일
Walter's Roberson mentioned that it might have error due to different number of columns.
Do you mind provide few of your files for me to try out?

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2018년 10월 5일

1 개 추천

The below code assumes the files are to be put side by side, and assumes that the number of rows in the files might be different. The number of columns in each file does not need to be the same at all. Shorter rows are padded with the value of your choice.
pad_value = nan; %change to 0 if you want padding by 0
%put files to combine
File = {file_name_1, file_name_2,file_name_3};
for index = 1 : numOfFile
newA = load(File{index});
if ndims(newA) > 2
error('File "%s" has more than 2 dimensions', File{index});
end
if index == 1
A = newA;
else
[or, oc] = size(A);
[nr, nc] = size(newA);
if nr > or %new file has more rows than any previous file
A(or+1:nr, :) = pad_value;
or = nr;
end
if nr < or %new file has fewer rows than some previous file
newA(nr+1:or, :) = pad_value;
nr = or;
end
A(:, end+1:end+nc) = newA;
end
end
% save final output
save('outputFile.txt', 'A', '-ascii', '-double')
The logic would be much the same for the case of putting the files under each other but not assuming that the number of columns are the same.
Raghunandan V
Raghunandan V 2018년 10월 5일

0 개 추천

Since you are trying to do in arrays you are not able to concancate You should try it using cell arrays. something like this
fileName={'new1.txt', 'new2.txt', 'new3.txt'};
%open file identifier
fid=fopen('MyFile.txt','w')
for k=1:length(fileName)
%read the file name as string including delimiters and next lines
List=textread(fileName{1,k},'%s','delimiter','\n');
%arrange them in order of k if you want in a cell array
FinalList{k,1}=List;
%or print them into a file.
fprintf(fid, [cell2mat(List) '\n']);
end
%close file indentifier
fclose(fid)

카테고리

도움말 센터File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

질문:

2018년 10월 4일

답변:

2018년 10월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by