Excel export script help request

2 visualizzazioni (ultimi 30 giorni)
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');

Risposta accettata

Jamie Rodgers
Jamie Rodgers il 1 Lug 2012
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 Commento
Tomasz Mlynowski
Tomasz Mlynowski il 3 Lug 2012
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

Accedi per commentare.

Più risposte (1)

Yash
Yash il 30 Giu 2012
Modificato: Walter Roberson il 1 Lug 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);

Community Treasure Hunt

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

Start Hunting!

Translated by