Effacer les filtres
Effacer les filtres

How to use variables from other functions/load issue

7 vues (au cours des 30 derniers jours)
Joshua Rodgers
Joshua Rodgers le 15 Juil 2020
Commenté : Joshua Rodgers le 15 Juil 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!
  2 commentaires
Joshua Rodgers
Joshua Rodgers le 15 Juil 2020
Hi Stephen, tried this and got it working straight away, thanks for the help!

Connectez-vous pour commenter.

Réponses (1)

KLR
KLR le 15 Juil 2020
Modifié(e) : KLR le 15 Juil 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 commentaire
Stephen23
Stephen23 le 15 Juil 2020
Modifié(e) : Stephen23 le 15 Juil 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Develop Apps Using App Designer dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by