MATLAB CSV read error - matlab

At first, I would like to say that this is not a duplicate of this: Reading CSV files with MATLAB?
In my script, I am trying to read a CSV file (sensor data). It is of the format:
2015-10-08 01:00:00.000,-0.762,-0.68,-0.234
2015-10-08 01:00:00.013,-0.762,-0.676,-0.234
2015-10-08 01:00:00.025,-0.762,-0.672,-0.234
2015-10-08 01:00:00.038,-0.762,-0.672,-0.23
and suddenly I get this error:
Error using dlmread (line 147)
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field number 1) ==>
,-0.02,-0.004,1.004\n
Error in csvread (line 48)
m=dlmread(filename, ',', r, c);
Error in getAvgData (line 17)
rawData = csvread([filePath, '/', fileList.name]);
Error in precomputeProcess (line 31)
getAvgData;
This code used to work well without this error and this is the first time I am seeing this. I am not sure if textScan would be of any help. here is my code snippet:
for k = 1:length(hourSampleRateList)
hourSampleRate = hourSampleRateList(k);
disp(['Start at sampling rate: ', num2str(hourSampleRate)]);
for hour = startHour:endHour
hourStr = num2str(hour,'%02i');
filePath = fullfile(pwd, username, 'MasterSynced', yearStr, monthStr,dayStr,hourStr);
fileList = dir([filePath, '/RawDataFloat*.csv']);
if (isempty(fileList))
continue;
end
rawData = csvread([filePath, '/', fileList.name]);
avgData = zeros(ceil(length(rawData)/hourSampleRate), 4);
j = 1;
for i = 1:hourSampleRate:length(rawData)-1;
avgData(j, :) = mean(rawData(i:i+hourSampleRate-1, :));
j = j + 1;
end
filePath = fullfile(pwd, username, 'MasterSynced', yearStr, monthStr,dayStr,hourStr);
myPath = [filePath, '\Avg', num2str(hourSampleRate, '%03i'), '-', fileList.name];
if exist(myPath, 'file') == 2
delete(myPath);
end
dlmwrite(myPath,avgData,'delimiter',',','precision','%.3f');
disp(['Day-Hour(', dayStr, '-', hourStr, '): completed.']);
end
end
Any help or info would be helpful.

M = csvread(filename) reads a comma-separated value (CSV) formatted
file into array M. The file must contain only numeric values.
Your first column is a date, not a numerical value.
M = csvread(filename,R1,C1) reads data from the file starting at row
offset R1 and column offset C1.
So you may skip the first column by using rawData = csvread([filePath, '/', fileList.name],0,1);
Also if your version is < 2016b, this MATLAB Answer suggests to use textscan instead, with 'HeaderLines',8 and appropriate format discriptor, 'Delimiter', and if necessary, 'EndOfLine' declarations.

Related

CSV file export in MATLAB adding new lines

I'm trying to take in a complex csv file in MATLAB and edit/rearrange it. I used tableread to pull out the numerical data that I then manipulated. Afterwards I aimed to take the first 52 rows of text metadata, add three more rows then combine with the numerical data and output as a csv file.
However, I can't seem to find a way to work with the text metadata rows that doesn't ultimately add new lines between them when output.
e.g. this is how my csv outputs:
#HEADER
,,,,,,
Instrument, Data Processed Analysis
,,,,,
Version,1.3.1,1.25
,,,,
Created,Monday, August 16, 2021 3:13:38 PM
,,,
Filename,D5-116.NGXDP
,,,,,
When it should look like:
#HEADER
NGX, Data Processed Analysis
Version,1.3.1,1.25
Created,Monday, August 16, 2021 3:13:38 PM
Filename,D5-116.NGXDP
Here is my code:
`
close
clear
fileName = 'D5-116.NGXDP';
fileOut = fileName(1:end-6);
fileOut = append(fileOut, '.csv');
eEnergy = 1.602176634e-19; %electron energy in coulombs
resisTor = 1e11; %Resistor value for EM
%% Reading and Edit Mass Data - this is too hardcoded right now
T = readtable(fileName, 'FileType', 'text');
T = T{:,:}; %convert from table to matrix
dataOrg(:,1:3) = T(1087:1236, 1:3); %cycle:time:Ar
dataOrg(:,4) = T(935:1084, 3); %39
dataOrg(:,5) = T(783:932, 3); %38
dataOrg(:,6) = T(631:780, 3); %37
dataOrg(:,7) = T(479:628, 3); %36
dataOrg(:,7) = dataOrg(:,7) * eEnergy * resisTor; %36Ar - convert CPS to V
dataOrg(:,3:7) = dataOrg(:,3:7) * 1000; %Convert all to mV
%% Reading the metadata from the file
T2 = fopen(fileName);
metaData = fread(T2, '*char')'; %read content
fclose(T2);
results = strsplit(metaData, '\n'); %split the char data by entry spaces
metaNeat = results(:,1:52)'; %Takes the metadata prior to #collectors
metaNeat(end+1, 1) = {'Blocks, 1'}; %added data
metaNeat(end+1, 1) = {'Cycles, 150'}; %added data
metaNeat(end+1, 1) = {'Cycle, Time, 40, 39, 38, 37, 36'};
for n = 1:length(metaNeat) %scan each row, if there is a comma split them
if contains(metaNeat(n,1), ',') == 1
tempMeta = split(metaNeat(n,1), ',')';
metaNeat(n,1:size(tempMeta, 2)) = tempMeta;
clear tempMeta
end
end
outPut = metaNeat; %metadata section
outPut(end+1:(end+150), 1:7) = num2cell(dataOrg); %mass value section
writecell(outPut,fileOut,'Delimiter',',', 'FileType', 'text')
I'm sure there is a way to do this in three lines of course. My guess is that the strsplit function I'm using to separate the char cell into multiple rows is preserving the \n value. The added lines (e.g. Blocks, 1) do not show gaps in the csv file. Any tips or advice would be appreciated.
Thanks for your time,

Matlab Load from relative path

function []= read_c3d_feat(output_list_relative)
dir_list = importdata(output_list_relative);
dim_feat = 512;
for i = 1 : size(dir_list, 1)
dir_str = char(dir_list(i));
feat_files = dir([dir_str, '/*.res5b']);
num_feat = length(feat_files);
feat = zeros(num_feat, dim_feat);
for j = 1 : num_feat
feat_path = strcat(dir_str, '/', feat_files(j).name);
...............
....................so on
Give me error like
Error using dir
Invalid path. The path must not contain a null character.
Error in read_c3d_feat (line 12)
feat_files = dir([dir_str, '/*.res5b']);
Your dir_list variable must have strings which contain null characters, as the error tells you. If you try using hard-coded strings you will see it works:
function read_c3d_feat(output_list_relative)
dir_list = {'21';'45';'18'};
for i = 1:size(dir_list, 1)
dir_str = dir_list{i}; % Loops through '21','45','18'
% The dir function now works because we know dir_str is a valid string
feat_files = dir([dir_str, '/*.res5b']);
end
end
This means you need to debug your code and find out what this line is actually assigning to dir_list:
dir_list = importdata(output_list_relative);
Note that if dir_list is a cell of text entries, you should be indexing it with curly braces as above. If instead it is a matrix (because all of the entries seem to be numerical anyway) then you should be using num2str when passing to dir:
function read_c3d_feat(output_list_relative)
dir_list = importdata(output_list_relative);
dim_feat = 512;
for i = 1:size(dir_list, 1)
feat_files = dir([num2str(dir_list(i)), '/*.res5b']);
% ...

Matlab; how to extract information from a header's file (text file)

I have many text files that have 35 lines of header followed by a large matrix with data of an image (that info can be ignored and do not need to read it at the moment). I want to be able to read the header lines and extract information contained on those lines. For instance the first few lines of the header are..
File Version Number: 1.0
Date: 06/05/2015
Time: 10:33:44 AM
===========================================================
Beam Voltage (-kV) = 13.000
Filament (W) = 4.052
Cond. (-kV) = 8.885
CenterX1 (V) = 10.7
CenterY1 (V) = -45.9
Objective (%) = 71.40
OctupoleX = -0.4653
OctupoleY = -0.1914
Angle (deg) = 0.00
.
I would like to be able to open this text file and read the vulue of the day and time the file was created, filament power, the condenser voltage, the angle, etc.. and save these in variables or send them to a text box on a GUI program.
I have tried several things but since the values I want to extract some times are after a '=' or after a ':' or simply after a '' then I do not know how to approach this. Perhaps reading each line and look for a match of a word?
Any help would be much appreciated.
Thanks,
Alex
This is not particularly difficult, and one of the ways to do it would be to parse line-by-line as you suggested. Something like this:
MAX_LINES_TO_READ = 35;
fid = fopen('input.txt');
lineCount = 0;
dateString = '';
beamVoltage = 0;
while ~eof(fid)
line = fgetl(fid);
lineCount = lineCount + 1;
%//check conditions for skipping loop body
if isempty(line)
continue
elseif lineCount > MAX_LINES_TO_READ
break
end
%//find headers you are interested in
if strfind(line, 'Date')
%//find the first location of the header separator
idx = find(line, ':', 1);
%//extract substring starting from 1 char after separator
%//note: the trim is to get rid of leading/trailing whitespace
dateString = strtrim(line(idx + 1 : end));
elseif strfind(line, 'Beam Voltage')
idx = find(line, '=', 1);
beamVoltage = str2double(line(idx + 1 : end));
end
end
fclose(fid);

MATLAB: Get the last modification time of a file

I am looking for MATLAB code that does some routine (updates a file.m), if file.csv is edited more recently than file.m.
Something that should look like:
% Write time extraction
tempC = GetFileTime('file.csv', [], 'Write');
tempdateC = tempC.date
tempM = GetFileTime('file.m', [], 'Write');
tempdateM = tempM.date
% Write time comparison
if numel(dir('file.m')) == 0 || tempdateC >= tempdateM
matDef = regexprep(fileread('file.csv'), '(\r\n|\r|\n)', ';\n');
f = fopen('file.m', 'w');
fwrite(f, ['Variable = [' matDef(1:end) '];']);
fclose(f);
end
The lines for timestamp extraction seem to be incorrect MATLAB code. The rest works (Evaluate variables in external file strings).
You can extract the modification time of a file using MATLAB's dir command. Something like:
function modTime = GetFileTime(fileName)
listing = dir(fileName);
% check we got a single entry corresponding to the file
assert(numel(listing) == 1, 'No such file: %s', fileName);
modTime = listing.datenum;
end
Note that the output is in MATLAB's datenum serial date format.

Error while trying to open files in Matlab

My code has 2 parts. First part is an automatic file opening programmed like this :
fichierref = 'H:\MATLAB\Archive_08112012';
files = dir(fullfile(fichierref, '*.txt'));
numberOfFiles = numel(files);
delimiterIn = ' ';
headerlinesIn = 11;
for d = 1:numberOfFiles
filenames(d) = cellstr(files(d).name);
end
for i=1:numberOfFiles
data = importdata(fullfile(fichierref,filenames{i}),delimiterIn,headerlinesIn);
end
Later on, I want the user to select his files for analysis. There's a problem with this though. I typed the lines as follow :
reference = warndlg('Choose the files from which you want to know the magnetic field');
uiwait(reference);
filenames = cellstr(uigetfile('./*.txt','MultiSelect', 'on'));
numberOfFiles = numel(filenames);
delimiterIn = ' ';
headerlinesIn = 11;
It's giving me the following error, after I press OK on the prompt:
Error using cellstr (line 34)
Input must be a string.
Error in FreqVSChampB_no_spec (line 128)
filenames = cellstr(uigetfile('./*.txt','MultiSelect', 'on'));
Anyone has an idea why it's doing that?
You do not need the cellstr command for the output of uigetfile in 'MultiSelect' mode: the output is already in a cellarray form (see doc of uigetfile).