writetable error line183, the first argument must be a table

I use the original code getting from Matlab website, which is as follows.
And the error was ''Error using writetable, First argument must be a table (line183)".
My matlab version is R2020b
Cause the code is from the authority, it supposely does work. However, it does not work
T = table(['M';'F';'M'],[45 45;41 32;40 34],...
{'NY';'CA';'MA'},[true;false;false])
writetable(T)
THIS IS THE SNAPSHOT

2 Comments

That error message doesn't look like a MathWorks written error message, and when I tried an experiment using release R2020b that's not the message I received. Can you confirm that you copied it verbatim from the Command Window into your Answers post? If you didn't copy it verbatim, could you please do so in a comment? Was this the error you received?
cd(tempdir)
writetable(42)
Error using writetable (line 248)
Unsupported type 'double'. Use writematrix instead.
the error I received is like
Error using writetable (line 183)
First argument must be a table.
>> writetable(T)

Sign in to comment.

 Accepted Answer

Use
which -all table
/MATLAB/toolbox/matlab/datatypes/tabular/@table/table.m % table constructor /MATLAB/toolbox/matlab/testframework/unittest/core/+matlab/+unittest/TestResult.m % Shadowed matlab.unittest.TestResult method /MATLAB/toolbox/matlab/testframework/unittest/core/+matlab/+unittest/+internal/BaseTestResult.m % Shadowed matlab.unittest.internal.BaseTestResult method /MATLAB/toolbox/matlab/bigdata/@tall/table.m % Shadowed tall method /MATLAB/toolbox/parallel/parallel/@distributed/table.m % Shadowed distributed method /MATLAB/toolbox/parallel/parallel/@codistributed/table.m % Shadowed codistributed method
to find out which function named table you are invoking. You should be seeing that tabuluar/@table/table.m as the first entry on the list, but it sounds as if you have some other function named table.m that is interfering.

12 Comments

>> which -all table
D:\Program Files\MATLAB\R2020b\toolbox\matlab\datatypes\tabular\@table\table.m % table constructor
D:\Program Files\MATLAB\R2020b\toolbox\matlab\bigdata\@tall\table.m % tall method
Thank you for your answer.
I tried, and this is the output.
the error still exists.
Odd. What shows up for class(T) just before you writetable(T) ?
And is it possible that there is some code between the time you create the table and the time you writetable() that is interfering?
... It wouldn't hurt to also check
which -all writetable
/MATLAB/toolbox/matlab/iofun/writetable.m /MATLAB/toolbox/matlab/bigdata/@tall/writetable.m % Shadowed tall method /MATLAB/toolbox/parallel/parallel/@codistributed/writetable.m % Shadowed codistributed method
I tried
>> class(T)
ans =
'table'
>> which -all writetable
D:\Program Files\MATLAB\R2020b\toolbox\matlab\iofun\writetable.m
D:\Program Files\MATLAB\R2020b\toolbox\matlab\bigdata\@tall\writetable.m % tall method
>> writetable(T)
Error using writetable (line 183)
First argument must be a table.
The error still exists.
I dont know what to do now.
I think there might be a function writetable that has the same name as a MATLAB builtin.
How could I use the the writetable function directly from MATLAB builtin?
In R, the language would be like "dplyr::select", which means I need the function "select" from the package "dplyr" instead of other package that may also has same name function "select".
Please check
which -all istable
/MATLAB/toolbox/matlab/datatypes/tabular/istable.m /MATLAB/toolbox/parallel/parallel/@distributed/istable.m % Shadowed distributed method /MATLAB/toolbox/parallel/parallel/@codistributed/istable.m % Shadowed codistributed method
the answer is
>> which -all istable
D:\iProgramFiles\toolbox\spm12\external\fieldtrip\compat\matlablt2013b\istable.m
D:\Program Files\MATLAB\R2020b\toolbox\matlab\datatypes\tabular\istable.m % Shadowed
God yes, it is shadowed.
How could I use the second one instead of the first one in one script, which contains code from toolbox/spm12 and also contain the code from matlab buildin toolbox ?
or, how could I fix it when I use 'writetable'?
You could try removing that compat directory from the MATLAB search path since you're using a release that has the istable function provided by MathWorks and so don't need the "compatibility" version from SPM12. If you try that and it works, you may want to provide feedback to the authors/maintainers of SPM12 that they should investigate if they can skip adding that compat directory to the MATLAB path when SPM12 is installed if the user is installing SPM12 in a new enough release.
thank you so much ! It worked
sorry to bother you again.
New error turned out.
it is
>> writetable(T)
Search term must be a text or pattern scalar.
I dont know what is this.
As an experiment:
restoredefaultpath; rehash toolboxcache
and then test with your table.
If that works, then you have added something on your MATLAB path that is interfering with MATLAB functions.
Yes, it worked.
But I do not know what interfered with MATLAB functions.
Toolboxes I added are psychtoolbox and SPM12
Did you delete D:\iProgramFiles\toolbox\spm12\external\fieldtrip\compat\matlablt2013b folder ?
Yes.
And after I run
restoredefaultpath; rehash toolboxcache
I added SPM12 toolbox again, and this time I delete ...compat\matlablt2013b and other similar compat\matlab2016b etc
Then I try writetable function.
It worked now.
Thank God, finally!
THANK YOU!!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020b

Asked:

on 24 Feb 2022

Commented:

on 4 Mar 2022

Community Treasure Hunt

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

Start Hunting!