How to use variables from other functions/load issue

25 visualizaciones (últimos 30 días)
Joshua Rodgers
Joshua Rodgers el 15 de Jul. de 2020
Comentada: Joshua Rodgers el 15 de Jul. de 2020
I'm new to Matlab and App Designer.
I'm creating an app which loads variables from a file chosen by the user, see code below. I then assign the variables to edit fields to load the current values into the app, this works.
function LoadFileButtonPushed(app, event)
[file,path] = uigetfile('*.mat');
filepath = [path, file];
load(fullfile(path,file));
app.VehicleMassEditField.Value = vehicle_mass;
... etc
end
There are other buttons which default the values back to what is currentlt saved on file, so requires the variables to be loaded again from the file using a different button and callback.
function DefaultButtonPushed(app, event)
% load("GUI/tire_data.mat");
load(fullfile(path,file));
app.VehicleMassEditField.Value = vehicle_mass;
...etc
end
However, it seems that once I have loaded the file, it seems to forget the path (uses the default path function) and file variables and I am presented with the error below.
Unrecognized function or variable 'file'.
Error in Test/DefaultButtonPushed (line 107)
file
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 382)
Error while evaluating Button PrivateButtonPushedFcn.
I would like to store the file path in the app so that when the app initially runs it will load the filepath previously used to load the variables.
I have tried to write the path and file variables to .txt and .xlsx and can't seem to get it working so any help on how to carry these variables over to the other functions would be greatly appriciated.
Thanks in advance!

Respuestas (1)

KLR
KLR el 15 de Jul. de 2020
Editada: KLR el 15 de Jul. de 2020
Well in order for the variables from one function to another in a GUI you would need to assign them to handles or pass them as global variables. Check this link for more and better explanation https://matlab.fandom.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
So in your code you would have to add a line as follows
function LoadFileButtonPushed(app, event)
[file,path] = uigetfile('*.mat');
filepath = [path, file];
load(fullfile(path,file));
app.VehicleMassEditField.Value = vehicle_mass;
handles.VehicleMass=vehicle_mass; % This is what you will have to pass in order to use it in the next function
... etc
end
function DefaultButtonPushed(app, event)
% load("GUI/tire_data.mat");
load(fullfile(path,file));
app.VehicleMassEditField.Value = vehicle_mass; % Instead of this you will use the handles saved
VehicleMassEditField=handles.VehicleMass
...etc
end
  1 comentario
Stephen23
Stephen23 el 15 de Jul. de 2020
Editada: Stephen23 el 15 de Jul. de 2020
"...you would need to assign them to handles or pass them as global variables"
Global variables are not recommended. Global variables should be avoided.
The handles structure is used in GUIDE, not App Designer.
For App Designer, the recommended method is to assign the values to object properties. This is clearly explained in the MATLAB documenation: "Using properties is the best way to share data within an app..."
Although popular with some users of this forum, the https://matlab.fandom.com/wiki/FAQ is out of date: it does not cover App Designer at all. The best, correct, always up-to-date source of information is the MATLAB documentation.

Iniciar sesión para comentar.

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by