How can I read in CSV files that were created by Excel on a Mac using TDFREAD?

3 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 25 Oct 2013
CSV files that are created by Excel on a Mac do not contain the proper linefeed characters at the end of each line. Unfortunately, this conflicts with the format that is required by TDFREAD.
Solution 1:
The most robust way to read in the data files in this case is to:
1) Open the “Import Data” UI
2) Highlight the data you would like to import, excluding the headers.
3) Ensure that “Column vectors” is highlighted.
4) Select the “Generate Function” option under the “Import Selection” button.
This will automatically create a function that can be called from any script to read the file you are interested in. This is the most robust way to read in the data of interest as you can rename the imported data to any name you would like each time you call the function instead of being limited to the names stored in the header of the data file.
Solution 2:
Another way to read the data in this case is to have MATLAB regenerate the files with the proper linefeed characters using the following code:
hf1 = fopen(input.txt');
hf2 = fopen('output.txt');
while( feof(hf1) == 0 );
fprintf(hf2,'%s\n',fgetl(hf1));
end
fclose(hf1);
fclose(hf2);
where ‘input.txt’ and ‘output.txt’ are the original CSV file and the corrected CSV file, respectively. After you have generated the ‘output.txt’ file using this code, you will be able to read in the data properly using TDFREAD using:
data = tdfread('output.txt',',');

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2013b

Community Treasure Hunt

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

Start Hunting!