Error using readtable (line 318) Matlab R2021a

15 views (last 30 days)
Zoé
Zoé on 28 Aug 2023
Commented: Rik on 28 Aug 2023
I try to open my file and it answers me that
K>> t=readtable(sprintf('F:\\sub_%i.xlsx',n))
Error using readtable (line 318)
Search term must be a text or pattern scalar.
And when i only use the readtable function it says me that
K>> readtable
Error using readtable (line 318)
Not enough input arguments.
And when i open the readtable script, it opens that and the variable "ME" at the end seems to be the problem since it is not defined anywhere
function t = readtable(filename,varargin)
[varargin{1:2:end}] = convertStringsToChars(varargin{1:2:end});
names = varargin(1:2:end);
try
if any(strcmpi(names,"Format"))
t = matlab.io.internal.legacyReadtable(filename,varargin);
else
func = matlab.io.internal.functions.FunctionStore.getFunctionByName('readtable');
C = onCleanup(@()func.WorkSheet.clear());
t = func.validateAndExecute(filename,varargin{:});
end
catch ME
throw(ME)
end
If someone could help me it eould be really great,
thanks!
  4 Comments
Cris LaPierre
Cris LaPierre on 28 Aug 2023
Your code runs without errors here. I also checked in R2021a and again, it ran without error.
Can you provide more details about your system and setup? Perhaps try restarting MATLAB and seeing if just running the code below works for you as well?
n=1;
t=readtable(sprintf('sub_%i.xlsx',n))
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
t = 240×6 table
Condition Target_RT Target_OnsetTime Target_RTTime Run RTTime_Onset _________ _________ ________________ _____________ _____ ____________ {'2'} 653 43614 44267 {'1'} 0.653 {'3'} 549 47613 48162 {'1'} 4.548 {'1'} 803 50613 51416 {'1'} 7.802 {'3'} 321 53579 53900 {'1'} 10.286 {'2'} 426 65577 66003 {'1'} 22.389 {'2'} 375 70577 70952 {'1'} 27.338 {'2'} 484 73576 74060 {'1'} 30.446 {'1'} 604 76576 77180 {'1'} 33.566 {'2'} 370 80576 80946 {'1'} 37.332 {'1'} 658 85575 86233 {'1'} 42.619 {'2'} 358 88574 88932 {'1'} 45.318 {'1'} 582 96573 97155 {'1'} 53.541 {'2'} 449 1.0559e+05 1.0604e+05 {'1'} 62.424 {'2'} 374 1.1259e+05 1.1296e+05 {'1'} 69.348 {'3'} 392 1.1859e+05 1.1898e+05 {'1'} 75.365 {'1'} 240.9 1.2259e+05 1.2283e+05 {'1'} 79.214
function t = readtable(filename,varargin)
[varargin{1:2:end}] = convertStringsToChars(varargin{1:2:end});
names = varargin(1:2:end);
try
if any(strcmpi(names,"Format"))
t = matlab.io.internal.legacyReadtable(filename,varargin);
else
func = matlab.io.internal.functions.FunctionStore.getFunctionByName('readtable');
C = onCleanup(@()func.WorkSheet.clear());
t = func.validateAndExecute(filename,varargin{:});
end
catch ME
throw(ME)
end
end
Rik
Rik on 28 Aug 2023
N=1;
for n=1:N
s = sprintf('sub_%i',n);
t=readtable(sprintf('sub_%i.xlsx',n),'NumHeaderLines',0, 'ReadVariableNames',true);
end
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
As you can see, this code runs without error on R2023a.
What does the line below return on your machine?
clc,strrep(which('readtable','-all'),matlabroot,'')
ans = 2×1 cell array
{'/toolbox/matlab/iofun/readtable.m' } {'/toolbox/shared/io/general/+matlab/+io/@ImportOptions/ImportOptions.m'}

Sign in to comment.

Answers (1)

dpb
dpb on 28 Aug 2023
Edited: dpb on 28 Aug 2023
t=readtable('sub_1.xlsx');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
head(t)
Condition Target_RT Target_OnsetTime Target_RTTime Run RTTime_Onset _________ _________ ________________ _____________ _____ ____________ {'2'} 653 43614 44267 {'1'} 0.653 {'3'} 549 47613 48162 {'1'} 4.548 {'1'} 803 50613 51416 {'1'} 7.802 {'3'} 321 53579 53900 {'1'} 10.286 {'2'} 426 65577 66003 {'1'} 22.389 {'2'} 375 70577 70952 {'1'} 27.338 {'2'} 484 73576 74060 {'1'} 30.446 {'1'} 604 76576 77180 {'1'} 33.566
t=readtable('sub_1.xlsx','numheaderlines',0,'readvariablenames',1);
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
head(t)
Condition Target_RT Target_OnsetTime Target_RTTime Run RTTime_Onset _________ _________ ________________ _____________ _____ ____________ {'2'} 653 43614 44267 {'1'} 0.653 {'3'} 549 47613 48162 {'1'} 4.548 {'1'} 803 50613 51416 {'1'} 7.802 {'3'} 321 53579 53900 {'1'} 10.286 {'2'} 426 65577 66003 {'1'} 22.389 {'2'} 375 70577 70952 {'1'} 27.338 {'2'} 484 73576 74060 {'1'} 30.446 {'1'} 604 76576 77180 {'1'} 33.566
Seems just fine although as shown you don't need either input parameter for the "plain vanilla" file...I wondered if the two of telling it no header lines and to read variable names were, perhaps, fighting each other, but it appears it figured it out.
As for the filename generation, I'd suggest using something more like
dataDir='YourDataFolder';
d=dir(fullfile(dataDir,'sub*.xlsx')); % return the dir() struct matching files
for i=1:numel(d) % iterate over collection
t=readtable(fullfile(d(i).folder,d(i).name); % read each in turn
... do whatever with each here...
end
Whatever was the cause of the error in the original, it wasn't in what we've been able to see that you've posted.
Would need the actual script and data file that created the error to diagnose; what we can see is ok...

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!