Error using ==> horzcat CAT arguments dimensions are not consistent.
조회 수: 1 (최근 30일)
이전 댓글 표시
Hey,
I am trying to convey several single .txt files into a large one with the following code:
allData=cell(2,1);
dataFiles=dir('subj/*.txt');
allDatabySubj = [];
for curr_fileID=1:length(dataFiles)
currFile=['subj/' dataFiles(curr_fileID).name];
[A, B, C, D, E, F, G] = textread(currFile, '%s %s %s %s %s %s %s', ...
'headerlines', 4) ;
*currdata = [A, B, C, D, E, F, G];*
allDatabySubj = [allDatabySubj; currdata];
end
And getting the following error related to the marked line:
??? Error using ==> horzcat CAT arguments dimensions are not consistent.
I do not understand why Matlab is complaining...
Here my data structure for the single files:
Datastructure:
VPID SESSID DATE RESP CRESP ACC RESPT 9999 3 16.2.2020 3 3 1 3000 9999 3 16.2.2020 4 5 0 4000
Thanks already!
Best,
Juls
댓글 수: 0
답변 (2개)
Image Analyst
2014년 7월 6일
That is not the way I told you to do it in your duplicate question: http://www.mathworks.com/matlabcentral/answers/140537#answer_143848. Why did you not take my advice? My advice here is the same so you probably won't take it again. There is no need or reason to read the data into arrays and concatenate them if you just want to combine several text files into one.
댓글 수: 1
Image Analyst
2014년 7월 7일
I explained how to do it but I didn't give you explicit code because I thought you could do it and it sounded like a homeowrk assignment. If you need code and it's not your homework, try this. It might need some debugging but it's basically correct.
fOutput = fopen('output.txt, 'wt');
myFolder = 'C:\Users\yourUserName\My Documents';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.txt');
textFiles = dir(filePattern);
for k = 1:length(textFiles)
baseFileName = textFiles(k).name;
fullInputFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullInputFileName);
fInput = fopen(fullInputFileName, 'rt');
% Read first three lines and ignore
line1 = fgetl(fInput);
line2 = fgetl(fInput);
line3 = fgetl(fInput);
inputTextLine = fgetl(fInput); % Get 4th line.
% Read the rest of the lines.
while ischar(inputTextLine)
% Write to output
fprintf(fOputput, '%s\n', inputTextLine);
inputTextLine = fgetl(fInput); % Get next line.
end
fclose(fInput); % Close this input file.
end
fclose(fOutput); % Close output file.
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!