From: William on
I'm trying to write some code that will write a series of matrices to an Excel file, one matrix to a page. I can do so easily enough by giving each command a separate line, but I'd like to loop through the list of matrices. I'm using an eval command, but it's returning invalid syntax.

The longhand code (which works) is:
xlswrite('Shortlist Outputs.xls', transpose(beta), 'Beta', 'C5');

The relevant code for the looped eval command:

lastNmonths = [36 12 3];
for pdloop = 1:length(lastNmonths)

...

OutputList = {'alpha' 'Alpha'; ...
'beta' 'Beta';...
'TrackingError' 'TrError';...
'DFS_indicator_M' 'DFSPI';...
'InfoRatio' 'InfoRatio'};

for n = 1:size(OutputList,1)
eval['xlswrite(''Shortlist Outputs.xls'', transpose(' OutputList{n, 1} '), ' ...
strcat(OutputList{n, 2} int2str(lastNmonths(pdloop))) ', ''C5'')'];
end
end

1. Could anyone help work out where I've messed up the syntax?
2. Is there an easier way to write the outputs?

Thanks.
From: us on
"William " <billytea69(a)yahoo.com> wrote in message <i013fa$297$1(a)fred.mathworks.com>...
> I'm trying to write some code that will write a series of matrices to an Excel file, one matrix to a page. I can do so easily enough by giving each command a separate line, but I'd like to loop through the list of matrices. I'm using an eval command, but it's returning invalid syntax.
>
> The longhand code (which works) is:
> xlswrite('Shortlist Outputs.xls', transpose(beta), 'Beta', 'C5');
>
> The relevant code for the looped eval command:
>
> lastNmonths = [36 12 3];
> for pdloop = 1:length(lastNmonths)
>
> ...
>
> OutputList = {'alpha' 'Alpha'; ...
> 'beta' 'Beta';...
> 'TrackingError' 'TrError';...
> 'DFS_indicator_M' 'DFSPI';...
> 'InfoRatio' 'InfoRatio'};
>
> for n = 1:size(OutputList,1)
> eval['xlswrite(''Shortlist Outputs.xls'', transpose(' OutputList{n, 1} '), ' ...
> strcat(OutputList{n, 2} int2str(lastNmonths(pdloop))) ', ''C5'')'];
> end
> end
>
> 1. Could anyone help work out where I've messed up the syntax?
> 2. Is there an easier way to write the outputs?
>
> Thanks.

well...
firstly, you do NOT need EVAL at all(!)...
then, take your lengthy statement apart and use temp variables for each input to XLSWRITE, which makes debugging easier, eg,

for pdloop=1:n
xlsfile='file_name';
xlssheet='sheet_name';
xlsarray=array_content;
xlsrng=cell_range;
xlswrite(xlsfile,xlsarray,xlssheet,xlsrng);
end
% note: some of these may be taken out of the loop, of course...

us