I keep getting the same error when using readtable or readmatrix: Dimensions of arrays being concatenated are not consistent.

81 ビュー (過去 30 日間)
A = readtable('data_file.csv')
A = readmatrix('data_file.csv')
When I tell you I have tried everything. Uninstalled and reinstalled Matlab twice. Cleared my cache of all matlab knowledge. I cannot read in data from a csv or text file using readtable and readmatrix for the life of me. Somebody please help. I have tried doing the same exact things on other's computers and it works perfectly. I am convinced my readtable and readmatrix functions are broken.
  2 件のコメント
Steven Lord
Steven Lord 2025 年 11 月 11 日 3:48
Does the error occur only for this one particular .csv file (or a corresponding .txt file) or does it happen for all such files?
If you use writematrix to write a matrix to a file, can you then call readmatrix on that file to recreate the matrix? If you run the following lines of code, what happens?
cd(tempdir)
A = magic(5);
writematrix(A, 'samplefile.csv')
B = readmatrix('samplefile.csv');
isequal(A, B) % ought to be true
ans = logical
1
If this is specific to one particular file, have you checked that all of the rows in the file have the same number of elements?
Chase
Chase 2025 年 11 月 11 日 4:09
Hi, thank you so much for your response. I get this error on all such files. I have created many test files and they all give me the same error message. In running your code, I got the following:
Error using readmatrix (line 11)
Dimensions of arrays being concatenated are not consistent.
Error in
B = readmatrix('samplefile.csv');
^^^^^^^^^^^^^^^^^^^^^^^^^^^

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2025 年 11 月 11 日 6:01
編集済み: Stephen23 2025 年 11 月 11 日 6:37
You have likely shadowed the function HEIGHT() or WIDTH() or SIZE() or LENGTH() or NUMEL() or similar:
Tell us the exact output from these commands:
which -all height
which -all width
and we might be able to identify a shadowed function.
If no obvious shadowing then tell us what this prints:
try
B = readmatrix('data_file.csv');
catch ME
fprintf('Error ID: %s\n', ME.identifier);
fprintf('Message: %s\n', ME.message);
fprintf('Error occurred in: %s (line %d)\n', ME.stack(1).name, ME.stack(1).line);
end
or use
dbstop if caught error
to identify the line and function/file and error cause. Let us know where it stops!
For those who are wondering, line 11 of READMATRIX() simply consists of
throw(ME);
and rethrows errors that are caught in the actual functions doing the heavy-lifting.
  4 件のコメント
Stephen23
Stephen23 2025 年 11 月 11 日 6:39
編集済み: Stephen23 2025 年 11 月 11 日 6:49
which -all height
C:\Users\chase\OneDrive - Virginia Tech\Documents\MATLAB\height.m
Problem: you have created a function named HEIGHT which is shadowing the inbuilt HEIGHT function.
Solution: delete or rename your function.
Chase
Chase 2025 年 11 月 11 日 6:52
Stephen, I cannot begin to tell you how grateful I am for your assistance. I have been trying to figure this out for over a week now!
Thank you so much. Happy holidays to you and your family.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

製品


リリース

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by