Error with horzcat matrix
Show older comments
Hi everybody, I'm new with Matlab and I have an error when I'm trying to execute my code.
This program extracts data from several Excel files and writes it to other Excel file. There is a for function which read a specific range of values from all files and copies it into a matrix. I concatenate the actual Matrix A with the new column of values. The problem is that after eight iterations, it appears an error related to the matrix dimension. How can I solve it? I think that I have to resize the matrix to each iteration. The number of rows is fixed and only increases the number of columns by one each iteration.
Thanks in advanced. I added the code and the error message.
Raúl.
%Open multiple files from a folder
myFolder = 'path';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.xls');
xlsFiles = dir(filePattern);
%BOX INPUT
prompt={'Sheet:','Range:'};
dlg_title = 'Input values from Excel file to read';
num_lines = 1;
def = {'1','AD24:AD41'};
answer = inputdlg(prompt,dlg_title,num_lines,def);
d=str2double(answer(1));
c=cell2mat(answer(2));
A=[];
B=[];
for k = 1:length(xlsFiles)
baseFileName = xlsFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
drawnow; % Force display to updaclc
a = xlsread(fullFileName,d,c);
A=[A,a];
b = cellstr(baseFileName);
B=[B,b];
end
%BOX OUTPUT
prompt = {'Sheet:','Range:'};
dlg_title = 'Output values from Excel file to write';
num_lines = 1;
def = {'1','A2'};
answer = inputdlg(prompt,dlg_title,num_lines,def);
g=str2double(answer(1));
h=cell2mat(answer(2));
rstatus = xlswrite('path',B,g,'A1');
rstatus = xlswrite('path',A,g,h);
ERROR MESSAGE
??? Error using ==> horzcat CAT arguments dimensions are not consistent.
Error in ==> RVv5_home at 37
A=[A,a];
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!