Error with horzcat matrix

2 次查看(过去 30 天)
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];

采纳的回答

Image Analyst
Image Analyst 2013-2-22
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 个评论
Raúl
Raúl 2013-2-23
Thanks Image Analyst.
My problem was that "a" was double it may be "cell. Fixed and running.
BR,
Raúl.
Image Analyst
Image Analyst 2013-2-23
You're welcome. Then mark as "Accepted" if there are no further questions.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by