Using uigetfile to read in MATLAB array - index exceeds number of array elements

1 回表示 (過去 30 日間)
Rebecca Ward
Rebecca Ward 2021 年 2 月 17 日
コメント済み: Rebecca Ward 2021 年 2 月 17 日
Hi, I'm trying to do somthing that should be quite simple :)
I just want to read in a *.mat file which contains an array using uigetfile and then plot two of the columns of the array against each other.
This is the code:
function ReadDataButtonPushed(app, event)
[file, path] = uigetfile;
if isequal(file,0)
msgbox('Please input data')
else
Data = load(fullfile(path, file));
X = Data(:,2);
Y = Data(:,3);
plot(app.UIAxes, X, Y);
end
I get the error message 'Index exceeds the number of array elements (1).'
Can someone tell me what I am doing wrong? Many thanks.

採用された回答

dpb
dpb 2021 年 2 月 17 日
The form of load with an assignment on the LHS returns the content of the .mat file in a struct with the variable names in the .mat file as the fields of the struct.
To load the variables themselves as in the .mat file, just use load w/o the assignment.
load(fullfile(path, file));
  3 件のコメント
dpb
dpb 2021 年 2 月 17 日
The variable names are embedded in the .mat file when it is SAVEd; you get them back automagically as above "for free" as whatever they were when the .mat file was created. This presumes, of course, that you knew then and know now what those variables are as does your code above that uses/expects a variable Data
If you don't know or want/need programmatic variables, then you revert back to the previous syntax -- the variables in the .mat file are returned in a struct by the name of the LHS assignment; that can be whatever you want (including a cell array for multiple copies, maybe). You dereference the variables from the struct by the dot notation.
See
doc load
doc struct
for the details of syntax.
Rebecca Ward
Rebecca Ward 2021 年 2 月 17 日
OK, thankyou that makes sense

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by