Read excel file where format and extension do not match into Matlab

7 visualizzazioni (ultimi 30 giorni)
Hi, I have excel files that appear as .xls in Windows but upon opening in Excel, the program opens a pop-up saying:
"The file format and extension of 'FILE' don't match. The file could be corrupted or unsafe. Unless you trust its source, dont open it. Do you want to open it anyway?".
I click 'yes' and then a banner appears at the top saying:
"Possible data loss: Some features might be lost if you save this workbook in the text (.txt) format. To preserve these features, save it in an Excel file format",
giving me the options to not show again or save as a different file type.
I have tried all the file-opening commands I am familiar with such as importdata, readcell, readmatrix, textscan, etc.
Error using readcell (line 152)
Unable to find or open '**EXAMPLEPATH**\**EXAMPLENAME**.xls'. Check the path and filename or file permissions.
Error using readcell (line 152)
Unable to open file '**EXAMPLEPATH**\**EXAMPLENAME**.xls' as a workbook. Check that the file exists, read access is available, and the
file is a valid spreadsheet file.
Error using xlsread (line 257)
File '**EXAMPLEPATH**\**EXAMPLENAME**.xls' not in Microsoft Excel Format.
Error using importdata (line 139)
Unable to open file.
Error using readtable (line 498)
Unable to open file '**EXAMPLEPATH**\**EXAMPLENAME**.xls' as a workbook. Check that the file exists, read access is
available, and the file is a valid spreadsheet file.
How can I import this file into Matlab?
EDIT: Removed filepaths/filenames
  3 Commenti
Stephen23
Stephen23 il 28 Dic 2021
Modificato: Stephen23 il 28 Dic 2021
"Hi, I have excel files ..."
No, you don't. You actually have some neatly formatted text files which look like this:
This file has absolutely nothing to do with Excel, apart from the misleading file extension. If you want to import text file data then clearly you will need to use text-file importing routines. Because the data are stored in blocks you will need to take that into account when importing:
Tip: a file extension is just some characters at the end of the filename, it does not actually determine the file type.
Stephen23
Stephen23 il 28 Dic 2021
Modificato: Stephen23 il 28 Dic 2021
" Do you have any recommendations on how to import this into MATLAB?"
The obvious step would be to rename the file using a more accurate file extension. Try that.
All of the READTABLE, READMATRIX etc. family of functions have an option that lets you specify the filetype (overwriding the automatic detection based on the file extension), the option is called "FileType". You could try using that:
But because of the repeating blocks of data, most likely you will have more luck with TEXTSCAN (which requires opening and closing the text file using FOPEN and FCLOSE), as the link I gave you earlier shows:
Note that you can easily call TEXTSCAN in a while loop together with FGETL, FSCANF, etc., checking for the end using FEOF:
fid = fopen(..);
while ~feof(fid)
... TEXTSCAN etc
end
fclose(fid);

Accedi per commentare.

Risposta accettata

Image Analyst
Image Analyst il 28 Dic 2021
It's a text file. Just rename it.
fileName = 'MA203901 ASP1 MR sta1-Idark.xls';
fileName2 = strrep(fileName, '.xls', '.txt');
copyfile(fileName, fileName2);
data = importdata(fileName2)

Più risposte (0)

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by