Why do I get an error when using the "readtable" function to import a tab-delimited file?

I have a system that generates tab-delimited files.  If I read the file into Excel, do some formatting, and write it out to a .xlsx file, I can read the .xlsx into MATLAB quite nicely with the "readtable" function.  However, if I try to use the "readtable" function on the raw tab-delimited .dat file, I get an error.
Here's the command I'm using and the error I get. in MATLAB R2016a:
>> data = readtable('myFile.dat','Delimiter','\t');
Error using readtable (line 143)
Cannot interpret data in the file 'myFile.dat'. Found 6 variable names but 5 data columns.
You may need to specify a different format string, delimiter, or number of header lines.
Is there a way to set the options so I can read this file with readtable?

 采纳的回答

Unfortunately, with these kinds of data, “readtable” function would not be able to satisfy your needs as it can’t deal with variable number of columns.  A potential workaround to this issue, which will import a table into MATLAB from your DAT file, is to use the import data tool available for MATLAB R2016a.  The interface is automatically invoked when you open the DAT file in MATLAB, or you can click on the “Import Data” button under the “VARIABLE” section of the “HOME” tab of the MATLAB window.
 
Steps to import a table into MATLAB from DAT file is as follows:
1.  Navigate to the folder containing your DAT file in MATLAB.
2. Type the following command in the Command Window:
>>open my_data.dat
3. The MATLAB import data interface will open.  There are five sections in the ribbon under the “Import” tab.  Under the “DELIMITERS” section, by default, data is read assuming the tab delimiter for columns.  You can adjust the delimiters accordingly here.
4. Press CTRL-A to select the entire spreadsheet for import, or select portions of the spreadsheet to import as you desire.
5.  Under the “IMPORTED DATA” section, select “Table” to import the spreadsheet into MATLAB as a table.  You may also import the selection as a set of column vectors, numeric matrix, or cell array by choosing the appropriate option under this section.
6. Under the “UNIMPORTABLE CELLS” section, you may choose to have MATLAB read empty fields as NaN (by default), or other placeholders as you desire.
7. Under each column heading, you may manually change the format of each column to have MATLAB import the column as “text”, “number”, or “date”, as needed.
8. Finally, if you wish to import the data into MATLAB now, you may click on the green checkmark button under the “IMPORT” section of the panel.  The interface will inform you the variable name of the table created.  In addition, you may also automate this process by generating a script or a function for this import operation.  Simply click on the drop down menu beneath the green checkmark, and select generate script, or generate function, as desired.
 

更多回答(0 个)

产品

版本

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by