필터 지우기
필터 지우기

fopen and xlswrite error

조회 수: 1 (최근 30일)
Jinsol
Jinsol 2013년 9월 4일
fod = fopen(outputfile,'w');
fprintf(fod, '%s \t%s \t%s \t%s \t%s \t%s', 'WS Corr 10m', 'WS Corr 21m', 'WS Corr 72m', 'WS Corr 138m', 'length', 'slopelength');
inputdata_c1=xlsread(output_c1,'C2:C442');
xlswrite(outputfile, inputdata_c1, 'A2:A442');
with this code I get the error, described below
Error using xlswrite (line 220)
Invoke Error, Dispatch Exception:
Source: Microsoft Excel
Description: excel cannot open the file '.xlsx' because the file format or file extension is not valid
Help File: xlmain11.chm
Help Context ID: 0
I also cannot open the created file.

답변 (2개)

David Sanchez
David Sanchez 2013년 9월 4일
close the file before attempting the its opening:
fod = fopen(outputfile,'w');
fprintf(fod, '%s \t%s \t%s \t%s \t%s \t%s', 'WS Corr 10m', 'WS Corr 21m', 'WS Corr 72m', 'WS Corr 138m', 'length', 'slopelength');
fclose(fod);
xlswrite(outputfile, inputdata_c1, 'A2:A442');
What is the value of your output_c1?
  댓글 수: 1
Jinsol
Jinsol 2013년 9월 4일
편집: Jinsol 2013년 9월 4일
I already tried that way
output_c1 is just another xlsx file I want to read.
I think the problem is something with fopen. I also can't open the created xlsx file with the same error 'cannot open the file because the file format or file extension is not valid'

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


Image Analyst
Image Analyst 2013년 9월 4일
Why are you creating the xlsx file with fopen(), only to use xlswrite() later to do the same thing (just to copy & paste column 3 of the input spreadsheet to column 1 of the output spreadsheet)? Why not just create the "WS Corr 72m" column in xlswrite() right from the start? Also, you are creating a text file with 6 column headers and then trying to add only 1 column, not 6 column, to the workbook file. Why do you have 6 column headers with only 1 column of data? Whatever you do with xlswrite will blow away what was there before, that is assuming you closed the file with fclose. Anyway, the source of your error is that you did not close the file with fclose() before you called xlswrite(). Or possibly you have an old version of Excel that doesn't understand the .xlsx file format and extension. But like I explained, that won't be all you need to fix.
  댓글 수: 2
Jinsol
Jinsol 2013년 9월 5일
I used xlswrite 6 times to add 6 columns from different excel file. Is this impossible? Does xlswrite blow away the things I did before? Than What code should I use to solve this problem?
Image Analyst
Image Analyst 2013년 9월 5일
I'm not 100% sure if it blows it away or not - the help doesn't say so I'd have to check. Regardless, you can create the whole thing in one single call to xlswrite() if you just construct your cell array correctly to begin with. Something like (untested)
ca(1,1) = {'WS Corr 10m'};
ca(1,2) = {'WS Corr 21m'};
ca(1,3) = {'WS Corr 72m'};
ca(1,4) = {'WS Corr 138m'};
ca(1,5) = {'length'};
ca(1,6) = {'slopelength'};
[rows, columns] = size(inputdata_c1);
ca(2:rows+1, 1:columns) = inputdata_c1
xlswrite(filename, ca, 'A1');

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

카테고리

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