Access ui edit field values and sharing them in different functions

7 views (last 30 days)
Hello everyone, i'm quite new to MATLAB and for my thesis project I am creating a program that allows you to simulate a cut on a stl file of a tibia, reshape the polygons and plot the new modified image when pushing a button.
The parameters needed to be entered are 4: height, posterior angle, lateral angle and rotation of the prosthesis.
Now I am creating the graphical interface and I have decided to assign each input to a numericField.
I have created a function to generate all the buttons and their callbacks, while I have another function where the calculations are performed.
The problem I am having is that of not being able to access the value inserted in the numeric field and to assign it to a variable that can be shared in several different functions in the same .m file.
I tried to use global variables but I ran into problems.
Thanks everyone in advance for the help, I am attaching my file with the functions.
  3 Comments
Matt
Matt on 21 Oct 2022
I think you need to use setappdata and getappdata.
For exemple you want to share the height value between your functions. First you use settappdata to create the height value and store it in your figure object (called fig1 in your code)
height_ini = 1;
setappdata(fig1,'height',height_ini);
Then you want to update this value when the user update the height field.
So you in the callback function you use setappdata again to update the value :
function numberChanged(NF_height,event)
valore = NF_height.Value;
setappdata(NF_height.Parent,'height',valore)
end
And finally you get the data when needed (in your calc function I guess) with
height = getappdata(obj,'height'); % where the variable obj is fig1 : the place where the data is stored in my exemple
EDOARDO RADAELLI
EDOARDO RADAELLI on 24 Oct 2022
Edited: EDOARDO RADAELLI on 24 Oct 2022
Tank you for your answer, this works with multiple variables at the same time?
Edit: I tried this approach but i don't understand what's the problem. I have 2 .m file, in the first file there's the program, in the second .m file are stored the functions like generateUI, calcs ecc.
In the generateUI function i use setappdata, but i cannot use getappdata in the other .m file in the program.
Another problem appears when i have to use getappdata in other functions like calcs (in the same .m file), i cannot specify the object fig1

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 21 Oct 2022
Edited: Cris LaPierre on 21 Oct 2022
I would suggest developing your app in app designer. Once there, see this page on how to share data within your app.
  2 Comments
EDOARDO RADAELLI
EDOARDO RADAELLI on 24 Oct 2022
I tried to use app designer but i'm new to matlab and i can't understand how it works, so i choose to manually create the components.
Cris LaPierre
Cris LaPierre on 24 Oct 2022
That is how I started building my apps, too. I have a couple suggestions. First, try to complete the built-in tutorial. Then, consider taking a look at the last chapter of MATLAB Essentials on edX, which covers building an app. You can enroll for free.

Sign in to comment.

Categories

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!