How can I get my function to make changes to my state button properties?

25 views (last 30 days)
Hello,
I am attempting to produce a script that will display a graph based on inputs typed in by the user. I decided display this in a graphical window so it would look nicer. I am attempting to have a button click change the button value and/or the operational state of other buttons depending on which button has been pressed. To do this I have used a ValueChangedFcn call back to initiate a function which I thought would allow me to do this. Unfortunately this has not been the case.
I have attched an example of my code below with most of the extra buttons removed, however they have all been written in the same way. I also added the text displays as an attempt to check that the function and if statement had been running. So far it appears that they both run, but do not change the value for my edit_inputs value.
Essentially, I am trying to have a window where the 'edit' button begins pressed. When the 'save' button is pressed I would like the edit button to then appear unpressed. I hope to include more buttons where are simialr effect is produced for the buttons operational state.
Apologies if I have made any obvious errors, I am attempting to get to a higher skill level than I was taught while studying. Any help and advice is very much appreciated. Thank you in advance.
%Window creation
fig = uifigure; %creates a window for inputting variables
%buttons
save_inputs = uibutton (fig,'state','Text','Save Inputs','ValueChangedFcn',@press); %creates button
save_inputs.Position = [80 170 100 22]; %positions button
save_inputs.Value = false; %sets the button as not pressed
edit_inputs = uibutton (fig,'state','Text','Edit Inputs'); %creates button
edit_inputs.Position = [80 140 100 22]; %positions button
edit_inputs.Value = true; %sets the button as pressed
%% creates a function for when a button is pressed
function [save_inputs,edit_inputs] = press(save_inputs,~)
disp('Function ran')
if save_inputs.Value == true
edit_inputs.Value = false; %sets the edit button to be unpressed
disp('Edit Button Unpressed')
end
end

Accepted Answer

Voss
Voss on 16 Feb 2024
The basic problem is that the variable edit_inputs is not defined inside the press function. In other words, there is currently no way to access the edit button inside the save button's callback.
[When you run your code as-is, the line edit_inputs.Value = false; creates a variable called edit_inputs in the press function's workspace which is a struct with one field (called "Value") whose value is false. This edit_inputs is not the same as the one in your script (i.e., the edit button) because the press function uses its own workspace separate from the script, which uses the base workspace. Thus the function runs without throwing an error, but it also doesn't do the right thing - the edit_inputs in press is discarded when press finishes.]
@Pavel Kurguz has shown one solution, which is to nest the press function inside a parent function containing the button variables (nested functions can access the variables in their parent function's workspace - see here).
Another solution is to pass the edit button as an input argument to the press function:
%Window creation
fig = uifigure; %creates a window for inputting variables
%buttons
save_inputs = uibutton (fig,'state','Text','Save Inputs'); %creates button
save_inputs.Position = [80 170 100 22]; %positions button
save_inputs.Value = false; %sets the button as not pressed
edit_inputs = uibutton (fig,'state','Text','Edit Inputs'); %creates button
edit_inputs.Position = [80 140 100 22]; %positions button
edit_inputs.Value = true; %sets the button as pressed
% specify the save button's ValueChangedFcn, with edit_inputs as additional
% input argument:
save_inputs.ValueChangedFcn = {@press,edit_inputs};
%% creates a function for when a button is pressed
function press(save_inputs,~,edit_inputs) % <- edit_inputs is included in input arguments
disp('Function ran')
if save_inputs.Value == true
edit_inputs.Value = false; %sets the edit button to be unpressed
disp('Edit Button Unpressed')
end
end
  2 Comments
Ciarán Gray
Ciarán Gray on 19 Feb 2024
Edited: Ciarán Gray on 19 Feb 2024
I see, that makes a lot of sense as to why nothing was happening. It seems very obvious now! Thank you for your help and taking the time to explain why my function wasn't working as intended.
Having tried both solutions I can confirm that both solutions make my example code work as intended.

Sign in to comment.

More Answers (1)

Pavel Kurguz
Pavel Kurguz on 16 Feb 2024
Hi, looks like this is what you want.
And this page will be very useful in order to implement your functionality in different ways: https://www.mathworks.com/help/matlab/creating_guis/write-callbacks-for-apps-created-programmatically.html
ThemeCopy
function myFunction
%Window creation
fig = uifigure; %creates a window for inputting variables
%buttons
edit_inputs = uibutton (fig,'state','Text','Edit Inputs'); %creates button
edit_inputs.Position = [80 140 100 22]; %positions button
edit_inputs.Value = true; %sets the button as pressed
save_inputs = uibutton (fig,'state','Text','Save Inputs','ValueChangedFcn',@(src,event)press); %creates button
save_inputs.Position = [80 170 100 22]; %positions button
save_inputs.Value = false; %sets the button as not pressed
%% creates a function for when a button is pressed
% function [save_inputs,edit_inputs] = press
function press
disp('Function ran')
if save_inputs.Value == true
edit_inputs.Value = false; %sets the edit button to be unpressed
disp('Edit Button Unpressed')
end
end
end
  1 Comment
Ciarán Gray
Ciarán Gray on 19 Feb 2024
This solution helps my script work as intended, thank you! The link you provided was also very helpful, thank you again for that.

Sign in to comment.

Categories

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

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!