How to import time from an excel file into matlab
2 次查看(过去 30 天)
显示 更早的评论
Hello,
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);
uiwait(warndlg(errorMessage));
return;
end
%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);
end
0 个评论
采纳的回答
John Doe
2019-10-7
编辑:John Doe
2019-10-7
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,theFiles.name));
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
end
csvread only works when the file contains only numeric values
Description
M = csvread(filename) reads a comma-separated value (CSV) formatted file into array M. The file must contain only numeric values.
4 个评论
Jeremy Hughes
2019-10-7
编辑:Jeremy Hughes
2019-10-7
PSA
I see this a lot on answers: detectImportOptions followed by undoing the things detectImportOptions does for you.
opts = detectImportOptions(filename);
opts.DataLine = 1;
opts.Delimiter =','; %Specifies that the data is comma seperated
Sometimes detectImportOptions will get things wrong, and the more messy the file the more the chance of that, so it accepts NV pair arguments to help make the detection smarter. (also faster). The following is almost the same result, but avoids the hard parts.
opts = detectImportOptions(filename,...
'Delimiter',',',...
'NumHeaderLines',0);
This will only really do detection of the names and the types of each variable.
John Doe
2019-10-7
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!
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!