Azzera filtri
Azzera filtri

Error using ==> horzcat CAT arguments dimensions are not consistent.

1 visualizzazione (ultimi 30 giorni)
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

Risposte (2)

Image Analyst
Image Analyst il 6 Lug 2014
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 Commento
Image Analyst
Image Analyst il 7 Lug 2014
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.

Accedi per commentare.


julro
julro il 7 Lug 2014
Hey,
your answer does not contain how to create a new txt file containing all the single file. The answer you gave was related to another question - how to delete the rows. I do not see what you are suggesting regarding the described problem. Sorry. Maybe you can give me a hint. Thanks.

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by