readtable error!!! Previously, readtable worked, but suddenly one day it started throwing an error.
30 views (last 30 days)
Show older comments
clear;clc;close all;
for num_file = 1
file_index = ['E:\dachaung(temp)\code\dataself\data_raw\test\2024-11-22_',num2str(num_file),'.csv']
database_length=get_data_length(file_index);
csi_matrix = read_file(file_index);
end
function reset = read_file(filename)
temp = readtable(filename);
function data_length = get_data_length(filename)
temp = readtable(filename);
Error using readtable(line 517) Inputs must be a string array, character vector, or a cell array of character vectors.
3 Comments
dpb
on 23 Nov 2024 at 14:22
A .csv file must be text; apparently something in this particular file doesn't live up to that requirement.
One possible cause could be that an Excel file got saved/renamed with the .csv extension and readtable infers the file type from that if the 'FileType' named parameter isn't used to force the file type if it doesn't match the presumption made from the extension.
Cris LaPierre
on 23 Nov 2024 at 14:48
Edited: Cris LaPierre
on 23 Nov 2024 at 15:44
In other questions like this, the user created a file that shadowed a routine used by readtable (e.g. replace). Follow the debugging suggestions in this thread:
This could also explain why the behavior changed suddenly.
Answers (1)
Image Analyst
on 23 Nov 2024 at 15:29
Edited: Image Analyst
on 23 Nov 2024 at 17:46
Try it this way:
[EDIT -- now tested with folder with CSV files in it]
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Define the folder.
% folderPrefix = pwd;
folderPrefix = 'E:\dachaung(temp)\code\dataself\data_raw\test\';
% Find out how many CSV files live in the folder.
fileList = dir(fullfile(folderPrefix, '*.CSV'))
totalNumberOfFiles = numel(fileList);
fprintf('Found %d CSV files in "%s".\n', totalNumberOfFiles, folderPrefix)
% Loop over each CSV file finding out how big it is.
rows = zeros(totalNumberOfFiles, 1);
columns = zeros(totalNumberOfFiles, 1);
for num_file = 1 : totalNumberOfFiles
baseFileName = sprintf('2024-11-22_%d.csv', num_file);
% baseFileName = fileList(num_file).name; % In general
fullFileName = fullfile(folderPrefix, baseFileName);
if isfile(fullFileName)
fprintf('Now reading file #%d of %d: "%s"\n', num_file, totalNumberOfFiles, fullFileName);
% Read CSV file into a table variable.
csi_matrix = read_file(fullFileName);
% Get the number of rows and columns in that table.
[rows(num_file), columns(num_file)] = get_data_length(csi_matrix);
fprintf('The above file has %d rows and %d columns.\n\n', rows(num_file), columns(num_file));
else
fprintf('File #%d of %d not found: "%s"\n', num_file, totalNumberOfFiles, fullFileName);
end
end
function t = read_file(fullFileName)
t = readtable(fullFileName);
end
function [rows, columns] = get_data_length(tableName)
[rows, columns] = size(tableName);
end
0 Comments
See Also
Categories
Find more on Standard File Formats 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!