Error with horzcat matrix
1 view (last 30 days)
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];
0 Comments
Accepted Answer
Image Analyst
on 22 Feb 2013
Put this just before the line:
[rows1 columns1] = size(A) % No semicolon
[rows2 columns2] = size(a) % No semicolon
Since you're stitching horizontally, the number of rows must be the same. rows1 = rows2. If they're not, you had better think a lot about what you're trying to do because something is not as you expect.
2 Comments
Image Analyst
on 23 Feb 2013
You're welcome. Then mark as "Accepted" if there are no further questions.
More Answers (0)
See Also
Categories
Find more on Annotations 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!