readtable error!!! Previously, readtable worked, but suddenly one day it started throwing an error.
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
dpb
le 23 Nov 2024
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
le 23 Nov 2024
Modifié(e) : Cris LaPierre
le 23 Nov 2024
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.
Réponses (1)
Image Analyst
le 23 Nov 2024
Modifié(e) : Image Analyst
le 23 Nov 2024
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 commentaires
Voir également
Catégories
En savoir plus sur Environment and Settings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!