How to import time from an excel file into matlab

4 views (last 30 days)
I'm currently just trying to import a csv file into matlab so that I may plot columns 1 and 2.
I have the following code below which checks for any CSVs files in a given directory and imports their data.
However, the problem I'm encountering is that given the format of the time in my CSV file, it yields an error claiming.
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field number 2) ==> :00:00,2\n
This problem is solved if I just skip the time column entirely. But I need to import the time and I can't just skip the time column.
Is there any way I can import the time given its current formatting? I have the CSV file attached to this post for reference.
The code I'm running:
myFolder = 'C:\Users\User1\Desktop\Search here\Batch CSVs';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
%Output file type to save plot as
filetype = '.fig';
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.csv'); % File extension to search for
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
%Creates two 2-d plots
dataArray = csvread(fullFileName,1,0); %Grab data starting from row 1 column 0
time = dataArray(:,1);
weight = dataArray(:,2);

Accepted Answer

John Doe
John Doe on 7 Oct 2019
Edited: John Doe on 7 Oct 2019
Use read table and specify your input format type. I recognise some of this code :).
If your data has no header / variable names. Then readtable will generate a variable name which corrsponds with the column position i.e first column woul be Var1.
Here is an example:
%opts.VariableNamesLine = 1; %Defines the row location of channel variable name
%opts.VariableUnitsLine = 2; %Defines the row location of channel units
opts = detectImportOptions(fullfile(myFolder,;
opts.DataLine = 1;
opts.Delimiter =','; %Specifies that the data is comma seperated
opts = setvaropts(opts,'Var1','InputFormat','MM/dd/uuuu HH:mm');
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
%Creates two 2-d plots
idt{k} = readtable(fullFileName, opts, 'ReadVariableNames', true); %imports data from csv
csvread only works when the file contains only numeric values
M = csvread(filename) reads a comma-separated value (CSV) formatted file into array M. The file must contain only numeric values.
John Doe
John Doe on 7 Oct 2019
I always thought that; yet I went with the general consensus of detectImportOptions first after some discussion on here.
Perhaps I'll change my ways!

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!