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

1 visualización (últimos 30 días)
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.

Respuesta aceptada

dpb
dpb el 17 de Feb. de 2021
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 comentarios
dpb
dpb el 17 de Feb. de 2021
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.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by