Read a file into app designer with a button and use later with another button

9 views (last 30 days)
James Browne
James Browne on 9 Jan 2022
Answered: Cris LaPierre on 11 Jan 2022
Hi,
I am tyring to load a .mat file into an app made with the app designer to be used in a later callback.
I wanted to have one button to load and other buttons to do calculations.
For example, if I want to load a .mat file that has A and B defined.
-------------------------EXAMPLE---------------------
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: LoadFileButton
function LoadFileButtonPushed(app, event)
load('file.mat')
end
% Button pushed function: calculateButton
function calculateButtonPushed(app, event)
A+B
end
end
-----------------------------------------
I wanted the resultant calculation to be A+B using the values of A and B from the original loaded file, however it doesn't seem to store the values of A and B for use with the later callback. Is there anyway to do this?

Answers (1)

Cris LaPierre
Cris LaPierre on 11 Jan 2022
You need to load your mat file to an app property.
Use the link above to create the property. Let's say you name it S.
Then, in your callback that loads the mat file, you need to use the syntax S = load(___) to load the mat file. Your code, then, might look like this.
% Button pushed function: LoadFileButton
function LoadFileButtonPushed(app, event)
app.S = load('file.mat');
end
Now when you want to use your variable in another callback, just remember they are in the structure app.S, so you might access it as follows (guessing here because we don't know what your mat file has in it). Note that I am assuming the result of the calculation is stored in another app property named calc.
% Button pushed function: calculateButton
function calculateButtonPushed(app, event)
app.calc = app.S.A + app.S.B;
end

Community Treasure Hunt

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

Start Hunting!