MATLAB Answers

0

Excel export script help request

Dear Matlab Users,
I have written code to create an export to excel file .m function. i believe it is correct code until----->
DataFinal = [date, name, Data]; where it has the error
Error using horzcat
CAT arguments dimensions are not consistent.
How do I solve this? What have I done incorrectly? How can I fix this? to get the excel file exported?
The code is below
Thank you
load djip1.mat
% list all individual stocks
aa = pricesMat(:,1);
axp = pricesMat(:,2);
% define length of name and date as N and T
N = length(name);
T = length(date);
%create company name series
tempname = cell(1, N);
for i = 1:N;
tempname = repmat(name,1);
end;
n = length(name); %no. of columns (no. of variables * no. of companies)
k = n/N; %no. of variables
Data = [];
for i = 1: N
tempdata = pricesMat;
Data = [Data; tempdata];
end;
DataFinal = [date, name, Data];
%ERROR appears here!!!!
%Export the organized data into excel files
TITLE = {'date','AA','AXP'};
xlswrite('dataFinal.xls',TITLE,'sheet3','A1')
xlswrite('dataFinal.xls',Name,'sheet3','A2');
xlswrite('dataFinal.xls',DataFinal,'sheet3','B2');

  0 Comments

Sign in to comment.

2 Answers

Answer by Jamie Rodgers on 1 Jul 2012
 Accepted Answer

I could be wrong - since I don't have access to you data, but - guessing - I think what you MAY be doing here is trying to concatenate a cell array (name) with a matrix (Data) and Matlab doesn't like that!
Also not clear to me whether 'date' is numeric or cell. Assuming that your date is a matrix, name is a cell, and Data is a matrix, try swapping the error line to this:
DataFinal = [num2cell(date), name, num2cell(data)]
or if 'date' is already a cell array
DataFinal = [date, name, num2cell(data)]
(NB id date is a character array, use cellstr to convert this to a cell array)
Failing that then you have the wrong number of rows in your variables. Put a breakpoint in at the line where you do the concatenation, and inspect the variables with the variable editor to see if they are the right size and shape.
I think I am right in saying that xlswrite will handle eithetr matrix data or cell data, but can't cope with a mixture of both. So either export your numeric and txt/cell data separately in 2 lines, or use num2cell to convert your matrix data to cell before using xlswrite.
Can't be sure, but that's my guess, and it can't hurt to try!

  1 Comment

thank you very much, easy to understand answer for a relative novice to the matlab world! I think you have nailed the problem on the head. however still having difficulties fixing it. i have tried your suggestion on the two new lines both do not work and still have the dame error.
date field is a cell, Data is a matrix, name is a cell array.
I think I have the wrong number of rows in my variables. must be the solution.
How do I initiate a breakpoint line to do a concatentation to inspect if variables the correct size and shape?
the reason for combining the matrix and cell data into one export function is i am doing pairs trading and will have a lot of results so trying to create an automatic export fucntion to save time.
thanks very much for your help so far

Sign in to comment.


Answer by Yash
on 30 Jun 2012
Edited by Walter Roberson
on 1 Jul 2012

see horzcat error come when you dont have the same number of rows in the variable you are concatenating horizontally so make sure they are same and separated by commas
like ill show a horzcat examples as
load XY.txt; % read data into the sector matrix
bigX=XY(:,2);
bigY=XY(:,1);
kstart=max(t1,t2)+1;
N=length(x)-kstart+1; %x
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%Find best FIT parameters for the series
for k=kstart:length(x)
p(k)=x(k-t1)/(1+x(k-t1)^c1);
p1(k)=x(k-1);
p2(k)=y(k-t2)/(1+y(k-t2)^c2);
p3(k)=y(k-1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
f=p(kstart:length(x));
f1=p1(kstart:length(x));
f2=p2(kstart:length(x));
f3=p3(kstart:length(x));
a=f'; % now transposing them becasue size of x is 519-1 and size of f was 1-519
a1=f1';
a2=f2';
a3=f3';
m=bigX(kstart:length(x));
bigX=horzcat(m,a,a,a2,a3);

  0 Comments

Sign in to comment.