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

조회 수: 27 (최근 30일)
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
dpb
dpb 2024년 11월 23일 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 2024년 11월 23일 14:48
편집: Cris LaPierre 2024년 11월 23일 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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2024년 11월 23일 15:29
편집: Image Analyst 2024년 11월 23일 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

카테고리

Help CenterFile Exchange에서 Standard File Formats에 대해 자세히 알아보기

태그

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by