xlswrite - not accepting stored string as valid 'A1' format for Excel

I am currently trying to create a script that will output to Excel a series of 1000x8 matrices. So far my script looks like this, but it is not accepting xlcolo as a valid input and I can't figure out why. If there is an easier way to do this please help out!
xlcol = {'A3'; 'J3'; 'S3'};
xlcoli = {'A'; 'B'; 'C'; 'D'; 'E'; 'F'; 'G'; 'H'; 'I'; 'J'; 'K'; 'L'; 'M'; 'N'; 'O'; 'P'; 'Q'; 'R'; 'S'; 'T'; 'U'; 'V'; 'W'; 'X'; 'Y'; 'Z'};
%Output to Excel every 1000 trials
if rem(n,1000) == 0
a = n/1000;
if n > 3000
while a > 3
a = a - 3;
end
xlcolo = sprintf('%c%s',xlcoli(floor(n/3000)),xlcol(a));
else
xlcolo = xlcol(a);
end
xlswrite(spreadsheet, data, 'Martingale', xlcolo);
data = zeros(1,8);
end
I am aware that the code is not lasting beyond 78000 trials at the moment and will have to rectify that in the future. But for the time being I cannot work out why it thinks xlcolo is not a valid 'A1' format. I even tried displaying xlcolo in the command window before running xlswrite and it returned 'A3'.
Any help is greatly appreciated.
Regards,
Sam.

댓글 수: 1

Note that the xlcol and xlcoli are not a part of the loop, they are just there so the reader can identify with the variables.

댓글을 달려면 로그인하십시오.

 채택된 답변

Sven
Sven 2012년 3월 1일
G'day Mowgli,
The problem is that xcolo is a cell, but it's being referenced with parentheses:
xlcol = {'A3'; 'J3'; 'S3'};
xlcolo = xlcol(1);
The code above will return xlcolo(3) as a cell, but you want the contents of xlcolo(3) (ie, a string). Replace:
xlcolo = xlcol(a);
with:
xlcolo = xlcol{a};
and I think you'll be fine.
Note that your sprintf() function also needs to use braces {} to reference xcol otherwise sprintf will error saying "string expected"

댓글 수: 1

Thanks for that, this was the first time I have really had to use strings before in MATLAB.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

제품

질문:

2012년 2월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by