readtable error!!! Previously, readtable worked, but suddenly one day it started throwing an error.

27 Ansichten (letzte 30 Tage)
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 Kommentare
dpb
dpb am 23 Nov. 2024 um 14:22
A .csv file must be text; apparently something in this particular file doesn't live up to that requirement.
As @Mathieu NOE says, w/o the file saying what that something is is not possible.
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
Cris LaPierre am 23 Nov. 2024 um 14:48
Bearbeitet: Cris LaPierre am 23 Nov. 2024 um 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 23 Nov. 2024 um 15:29
Bearbeitet: Image Analyst am 23 Nov. 2024 um 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

Kategorien

Mehr zu Standard File Formats finden Sie in Help Center und File Exchange

Produkte


Version

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by