Filter löschen
Filter löschen

fopen and xlswrite error

1 Ansicht (letzte 30 Tage)
Jinsol
Jinsol am 4 Sep. 2013
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.

Antworten (2)

David Sanchez
David Sanchez am 4 Sep. 2013
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 Kommentar
Jinsol
Jinsol am 4 Sep. 2013
Bearbeitet: Jinsol am 4 Sep. 2013
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'

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 4 Sep. 2013
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 Kommentare
Jinsol
Jinsol am 5 Sep. 2013
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 am 5 Sep. 2013
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');

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by