Error `Index exceeds the number of array elements` while loading .mat file

I tried to encapsulate load function. However when I ran test_load_data.m, an error occurred:
Index exceeds the number of array elements. Index must not exceed 1.
Error in load_my_data (line 97)
S = load(path_file);
^^^^^^^^^^^^^^^
Error in
load_my_data(pathFileRelative);
A minimal working example is attached for reproduction.
If I comment out
load_my_data(pathFileRelative);
and add
load(pathFileRelative);
in test_load_data.m,
it works well.

回答 (1 件)

Matt J
Matt J 2025 年 9 月 23 日
編集済み: Matt J 2025 年 9 月 23 日
You have created a variable named 'load' in this line,
load = 0; % 轴承的载荷(单位:马力)(可能是0、1、2、3马力)
and later also in,
load = str2double(tokens{1}{1});
This hides the load() function from Matlab within the workspace.

3 件のコメント

Matt J
Matt J 2025 年 9 月 23 日
Also, load_my_data references a variable called file_name which is never defined anywhere... Should that be path_file?
Kaiser
Kaiser 2025 年 9 月 23 日
Thank you sir! I shouldn't define load as a variable to override the builtin function. After I changed it to payload, the error is gone.
And you are also right about file_name, it should be path_file.
Matt J
Matt J 2025 年 9 月 23 日
I'm glad it got sorted out, but please Accept-click the answer to indicate that the issue has been solved.

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

リリース

R2025b

質問済み:

2025 年 9 月 23 日

コメント済み:

2025 年 9 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by