I'm trying to import a data set into Matlab without success. See attached file.
1 view (last 30 days)
Show older comments
It appears that the issue is with the scientific notation. I've attempted to use fscanf(), dlmread() and importdata(). It either imports an empty set or I receive an error message. The following is my attempted code:
% Mike Beeman
% Peakfit Electrochemistry Pathogen Meat Detection
clc
clear all
close all
%%Import Data
% fid = fopen('/Users/mbeeman6/Desktop/Bacteria Detection Research - Gale/Data/june 12th Crypto datatofidgetwithonmatlab.csv')
%
% C = fscanf(fid,'%e') %,'HeaderLines',6);
% fclose all
data = dlmread('/Users/mbeeman6/Desktop/Bacteria Detection Research - Gale/Data/june 12th Crypto datatofidgetwithonmatlab.csv')
Thanks in advanced for any help you can offer.
0 Comments
Answers (2)
per isakson
on 23 Jun 2017
Edited: per isakson
on 23 Jun 2017
One way
ffs = 'june 12th Crypto datatofidgetwithonmatlab.csv';
fid = fopen( ffs, 'r' );
fmt = [ '"', repmat('%f',1,60), '"' ]; % Include the '"'
cac = textscan( fid, fmt, 'Headerlines',5, 'CollectOutput',true, 'Delimiter',',' );
fclose(fid);
and inspect the result
>> v = cac{1};
>> whos v
Name Size Bytes Class Attributes
v 170x60 81600 double
Walter Roberson
on 23 Jun 2017
dlmread() cannot handle quoted strings. It also cannot text headers unless you are using roughly R2014a onwards and tell it to skip those lines.
as_string = fileread('june 12th Crypto datatofidgetwithonmatlab.csv');
as_string(as_string == '"') = [];
as_lines = regexp(as_string, '\r?\n', 'split');
column_headers = regexp(as_lines{4}, ',', 'split');
data_units = regexp(as_lines{5}, ',', 'split');
if length(column_headers) < length(data_units); column_headers(length(data_units)) = {''}; end %last column of headers might be missing
data_items = regexp(as_lines(6:end), ',', 'split');
data_cell = vertcat(data_items{:});
data_numeric = str2double(data_cell);
The key outputs here are column_headers (cell array of char vector), data_units (cell array of char vector), and data_numeric (numeric array).
Potentially you might want to go from there to
t = array2table(data_numeric, 'VariableNames', matlab.lang.makeUniqueStrings(matlab.lang.makeValidName(column_headers)));
t.Properties.VariableDescriptions = column_headers;
t.Properties.VariableUnits = data_units;
The Variable Names will not be the nicest, as they will come out as a mix of names like H3CryptoCavitationTest_SWV and names like x_17 . The x_* names are from columns whose headers were empty.
The variable descriptions and units will only be visible if you ask for the properties, or if you use summary(t)
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!