xls export problem

조회 수: 8 (최근 30일)
Hello Blower
Hello Blower 2011년 2월 18일
>> xls_1
xls_1 =
857_386.xls
>> xlswrite(xls_1,a) ??? Error using ==> xlswrite at 213 Invoke Error, Dispatch Exception: Source: Microsoft Office Excel Description: ???????????????????:
??????????????? ??????????? ??????????????: < > ? [ ] : ? *? ????????????? 218 ????? Help File: C:\Program Files\Microsoft Office\OFFICE11\1028\xlmain11.chm Help Context ID: 0

채택된 답변

Matt Tearle
Matt Tearle 2011년 2월 27일
OK, your problem is the special characters in the string. You can't have slashes and line breaks in a filename.
When you do xlswrite('xls_1',a); the filename is the literal string 'xls_1', which is why it writes to xls_1.xls. You need the filename stored in the char variable xls_1, so:
xlswrite(xls_1,a);
However, to do that, xls_1 has to be a valid filename string. This works:
xls_1 = sprintf('%i_%i.xls',x,y)
If you're ok with underscores in places of slashes in the dates, this also works:
xls_1 = sprintf('%i_%i_%s_%s.xls',...
x,y,regexprep(start_day,'/','_'),regexprep(end_day,'/','_'));

추가 답변 (5개)

Jan
Jan 2011년 2월 20일
I do not see the problem.
a = rand(5);
xls_1 = '857_386.xls';
xlswrite(xls_1, a);
=> The file 857_386.xls is created. Did you use "xlswrite xls_1 a" without parenthesis?!
  댓글 수: 2
Hello Blower
Hello Blower 2011년 2월 27일
Really thanks for all of you helping me on this.
actually, the code is to retrieve the input from user and apply them to the export xls file name.
The highlighted code is shown below, unfortunately, I found:
1) matlab will output
"xls_1 =
857_386_482/11/20109502/11/2011
46xls
>> xls_2
xls_2 =
386_482/11/20109502/11/2011
46xls
"
if my inputs are "x=857,y=386,start_day=02/11/2010,end_day='02/11/2011"
That the xls_1 is unable to be "857_386_02/11/2010_02/11_2011.xls"
2) it is even if I erase the start_day and end_day in xls_1, then xls simply equal to "857_386.xls"
xlswrite('xls_1',a); is unable to export the proper filename;
Please help.
***************************************************************
x=input('what is stock 1? [xxxx] ');
y=input('what is stock 2? [xxxx] ');
start_day=input('what is the start day? [mm/dd/yyyy] ','s');
end_day=input('what is the end day? [mm/dd/yyyy] ','s');
a=ones(3,3);
xls_1 = sprintf('%i%s%i%s%i%s%i%s\n',x,'_',y,'_',start_day,'_',end_day,'.xls');
xls_2 = sprintf('%i%s%i%s%i%s\n',y,'_',start_day,'_',end_day,'.xls');
xlswrite('xls_1',a);
xlswrite('xls_2',b);
************************************************************
Hello Blower
Hello Blower 2011년 2월 27일
thanks a lot

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


Hello Blower
Hello Blower 2011년 2월 20일
Help. I would like to use some variable to make a new excel filename. But xlswrite(filename, M) seems to be unable to read the filename of variable. For example, if I have set xls_1 = 857_386.xls, then run xlswrite(xls_1,a). The created xls file will be xls_1.xls instead of 857_386.xls. How can I solve it?
Thank you so much.
********************************************************** xlswrite(filename, M) writes matrix M to the Excel file filename. The filename input is a string enclosed in single quotation marks, and should include the file extension. The matrix M is an m-by-n numeric or character array. xlswrite writes the matrix data to the first worksheet in the file, starting at cell A1.
  댓글 수: 1
Jan
Jan 2011년 2월 20일
Please post the exact Matlab commands you have used.

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


Matt Tearle
Matt Tearle 2011년 2월 20일
Dare I ask... When you say you set xls_1 = 857_386.xls, you do mean xls_1 = '857_386.xls', right?
  댓글 수: 2
Hello Blower
Hello Blower 2011년 2월 27일
yes i use the "x=input('what is stock 1? [xxxx] ');" to retrieve the x value, and try to put it in the xls file with start_day and end_day as well.
Thanks for your help
Hello Blower
Hello Blower 2011년 2월 27일
Thanks a lot.

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


Cagri Ozcaglar
Cagri Ozcaglar 2011년 3월 4일
File name may be too long. Try to shorten the absolute path of the file by shortening folder names.

Chidiebere Brendan Obiechefu
Chidiebere Brendan Obiechefu 2019년 7월 29일
I had same problem. What solved mine was writing the excel file to a desktop location. I don't know if it was because I was trying to save on onedrive (which is what I use normally), or that the file path is too long.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by