Updating variable matrix values to static boxes in GUI !
Show older comments
Iam completely new to GUI and programming any correction to code is appreciated !
I am trying to assign the value of variable x in another matfile named "main_file_Run" to static boxs !
For eg: varible x =[32, 7] and keeps updating and i when i press pushbutton it should run the matlab file needs to assign the values upated in x matrix i.e 32 nd 7 to two respective static boxes with tag names text3 and tex4 respectively.
function pushbutton1_Callback(hObject, eventdata, handles)
% a= get(handles.main_file_Run(variable x),'string');
% (set(handles.text3,'string'))
main_file_Run();
I know i completely missed the logic here ! I am only able to run the program by calling the matfile but have no clue what to do to update the values from the variables x to the two static boxes .Firstly how do we load the varible ? should we save it as text file then kind of load into the call back of pushbutton ?
thanks in advance!
Answers (1)
Kevin Phung
on 3 May 2019
Edited: Kevin Phung
on 3 May 2019
make sure your function main_file_run() actually has an output, so in that function:
function x = main_file_run()
% blah blah blah
x = (some answer from calculations above)
end
then in your pushbutton callback:
function pushbutton1_Callback(hObject, eventdata, handles)
x = main_file_Run();
set(findobj(gcf,'Tag','text3'),'String',num2str(x(1)))
set(findobj(gcf,'Tag','text4'),'String',num2str(x(2)))
end
note :
- how the static text boxes display strings and not numerics.
- I'm finding the static textboxes through their given Tag properties (i hope you did mean this, and not their variable names defined in another function) in the current figure (gcf))
- For future reference, if you don't want to use gcf and want to look at a particular figure, you can try do do:
f = findobj(groot,'Name','FigureTitle') % returns figure handle of figure with specified name
16 Comments
chamant swarup
on 3 May 2019
Rik
on 3 May 2019
They keep updating where? The main_file_Run has no output arguments, so it doesn't return anything.
Why are you using a global?
Why are you starting your function with clear? It doesn't serve any purpose, since the workspace is already clean.
Why are you closing all windows and setting the format? You have at least a GUI open that might close because of this. I don't know if any of your current functions output something to the command window, but from the looks of it, none should.
chamant swarup
on 3 May 2019
Rik
on 3 May 2019
You should make that variable x an output of one of those functions. Then you can easily set it. Just like Kevin put in his answer.
chamant swarup
on 3 May 2019
Rik
on 3 May 2019
As you're probably using GUIDE, you don't need to use findobj to get the handles. The gcf function returns the handle to the current figure, as its documentation would have told you. Then the findobj function will return the handles of objects that satisfy certain conditions.
In this case you can probably use the code below instead:
function pushbutton1_Callback(hObject, eventdata, handles)
x = main_file_Run();
set(handles.text3,'String',num2str(x(1)))
set(handles.text4,'String',num2str(x(2)))
end
Kevin Phung
on 3 May 2019
Rik, thanks for the help explaining the reasoning behind my answer. I didnt notice that chamant was using GUIDE.
Chamant, I suggest you read into how to use a script vs a function. When you're running functions, they each have their own 'scope' of variables that are not necessarily the same as your base workspace. Think about how hectic it would be having to check if your variable 'a' defined in a script (base workbase) is consistent across all functions within your script. so having :
clear
close all;clc;
at the beginning of a function is a bit redundant. because at the start of any function, the workspace is already clear. in this case for callback functions, it's actually detrimental. there are two default inputs: object handle (That triggered the callback) and the 'event.' You would not want to clear those.
Anyway, to reiterate, the solution to your dilemma is to set an output for your function main_file_run() and to add the 3rd argument to your set() function which you did not include.
Rik
on 4 May 2019
Thanks for the insights kevin! Unfortunately the values still are not pulled into the static boxes !
Here is what i did
function X= main_file_Run
X =[x(1),x(2)];
end
and in call back section i wrote
function pushbutton1_Callback(hObject, eventdata, handles)
X = main_file_Run();
set(handles.text3,'String',num2str(X(1)))
set(handles.text4,'String',num2str(X(2)))
The even more strange thing is when i end the function there are many errors relating varfile but without end it works but not able to pull the values of X !
Rik
on 4 May 2019
I hope that is not the full contents of the main_file_Run function, because then x would not yet be defined. The callback looks like it is fine if you put it exactly like this.
About the end keyword: GUIDE is so old it is from the time where the convention was to leave your function unclosed. Over the last years this convention has flipped (e.g. to enable the use of nested functions). The two styles are incompatible, so either you need to use an end for every function in an m-file, or for none of them.
chamant swarup
on 4 May 2019
Rik
on 4 May 2019
Do you still have the close in your function? Because that will close the GUI, which will delete the object, causing this error. Otherwise, try closing all GUI windows and reopen it to force a refresh.
chamant swarup
on 4 May 2019
Rik
on 4 May 2019
You were inconsistent with where you put the end keywords. I fixed those in the version that I attached as an m file.
chamant swarup
on 4 May 2019
Edited: chamant swarup
on 4 May 2019
Rik
on 4 May 2019
Somewhere some function is closing your GUI. You can use the debugger to set a breakpoint and go through your code line by line to see where your GUI is being closed.
chamant swarup
on 5 May 2019
Categories
Find more on App Building in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
