필터 지우기
필터 지우기

Error with horzcat matrix

조회 수: 3 (최근 30일)
Raúl
Raúl 2013년 2월 22일
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