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!