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

1 次查看(过去 30 天)
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by