You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
GUIDE code from pre 2015 need to access the button press function and "programatically press the button" from a different m file
3 views (last 30 days)
Show older comments
Hey All
As the title suggests, I am dealing with a GUIDE file from a long time ago and I need to have it running and while its running I want to be able to access one of the functions called SaveSpectrumPlotButton_Callback() on line 1933
I have not been able to find how to access it from a different file or even create an output that I could maybe access in order to activate that button. I have shared the m file below
Any help would be much appreciated/
3 Comments
Steven Lord
on 17 Jul 2024
Let's take a step back. What is your workflow where you need to programmatically trigger this local function that's intended to be triggered by an interactive part of a workflow (actually clicking on the button)? Perhaps we can suggest how to achieve your goal in a different way.
Bera
on 17 Jul 2024
Hey Steven first off thanks. Secondly, I am a co-op working on this code that was given to me and to be frank its quite disorganized. In summary, the code itself is a live updating camera that feeds information to a spectroscope and makes a plot of the peak and then to save this data a button called save plot has to be saved. The issue arises that I am automating a giant process of quantum dots be excited and the light being extracted. So I have to implement this code into there i.e. I have to have the main function running while I can "progammitcally press" the save button every time I'm positioned on a new quantum dot
Accepted Answer
Fangjun Jiang
on 17 Jul 2024
In the .m file created for your GUIDE application, there is a function called SaveSpectrumPlotButton_Callback() defined. Currently, it is a local function. Other .m file outside of the FWHM_ver_Richard.m file can not call it.
I think you can cut the code for SaveSpectrumPlotButton_Callback() out and make it a separate SaveSpectrumPlotButton_Callback.m file. Then you can call this function from anywhere.
Calling it from other .m file will be equivalent as " programatically press the button".
32 Comments
Bera
on 17 Jul 2024
Would it be that simple though? Since doesn't the function have to be within the GUI so that the button properties behave as expected?
Bera
on 17 Jul 2024
@Fangjun Jiang Like doesn't it need a bunch of external properties to behave as expected?
Bera
on 17 Jul 2024
and the original file is a GUI so if I cut something out of it Im not fully sure I'll be able to press the save button
Fangjun Jiang
on 18 Jul 2024
Edited: Fangjun Jiang
on 18 Jul 2024
I am pretty sure it will work. Whether it is the best way is a different question.
You can even not make any changes to your original file. Just copy that function and make it a separate function and then try it.
Voss
on 18 Jul 2024
You'll need to provide the correct arguments to the SaveSpectrumPlotButton_Callback function.
% store the main figure handle when the program is run
hFig = FWHM_ver_Richard();
% then, when you call the plot button callback, provide the arguments it expects
SaveSpectrumPlotButton_Callback(hFig,[],guidata(hFig))
Bera
on 18 Jul 2024
@Voss Thanks Voss I was just going to ask though, the FWHM_ver_richard function in the original function file already has an output. Are you suggesting to call it in a new m file with a new output and if so will that assign it to the figure? Additionally, the inputs you provided are you saying I need to change those or are those the perimeters I would use for calling? Thanks again
Voss
on 18 Jul 2024
Edited: Voss
on 18 Jul 2024
@Bera You're welcome!
I am not suggesting to change anything about the m-file you posted. I am only saying that, if you copy the SaveSpectrumPlotButton_Callback function into its own m-file, with the function itself unchanged, as @Fangjun Jiang has suggested, then you need to call it with the proper inputs:
- the figure itself (which is called hObject in SaveSpectrumPlotButton_Callback)
- the second argument (called eventdata in SaveSpectrumPlotButton_Callback) is unused, so you can pass anything; I suggested to pass an empty array []
- the guidata of the figure (called handles in SaveSpectrumPlotButton_Callback)
Now, you point out that FWHM_ver_Richard has an output already, and that is true. Its output is the figure itself, referred to as handles.output in FWHM_OutputFcn. See the excerpt below (comments I added are preceded by "<----"):
% --- Outputs from this function are returned to the command line.
function varargout = FWHM_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% ...
varargout{1} = handles.output; % <---- the output of FWHM_ver_Richard is handles.output
And handles.output is set to hObject in FWHM_OpeningFcn, where in that context hObject refers to the figure. Here are the relevant lines of FWHM_OpeningFcn:
% --- Executes just before FWHM is made visible.
function FWHM_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure % <---- hObject is the figure
% ...
handles.output = hObject; % <---- so handles.output is the figure
% ...
Again, no changes need to be made to the code; you only need to store that output to some variable (I called it hFig) when FWHM_ver_Richard is run, and pass that same variable to SaveSpectrumPlotButton_Callback (along with guidata(hFig) as the third input).
Rik
on 19 Jul 2024
One minor note: hObject is the handle to the callback object (in this case the button). Passing in the figure may or may not work. You should probably pass in the button handle instead (it will be a field of the guidata).
Bera
on 19 Jul 2024
Fangjun Jiang
on 19 Jul 2024
I would suggest copying that function into SaveSpectrumPlotButton_Callback.m file to make it a local function of that file. Hope that is the end of the chain dependency.
Voss
on 19 Jul 2024
@Bera: @Rik is correct that hObject in the function SaveSpectrumPlotButton_Callback refers to the button object. However, passing the figure instead of the button will work in this particular case because of how hObject is used in SaveSpectrumPlotButton_Callback (it's only used in guidata - which behaves the same whether you pass a figure or a descendant of the figure - and it's passed to SaveGraph, which doesn't use it), so the syntax I suggested initially should work (provided SaveGraph is an accessible function, as you point out).
Image Analyst
on 21 Jul 2024
The callback function for the Save button, savebutton_Callback(hObject, eventdata, handles), would of course be in the main GUI's m-file. That callback function can either call code right there inside that function (no second m-file needed), OR could simple make a one line call to another m-file, savegraph.m, which has the code in it. If it's in a second function file, or even if it's in the main m-file, make sure that if the savegraph() function needs any input arguments to be passed to it that the input arguments are in scope so that savegraph() can see and use them.
Bera
on 21 Jul 2024
@image analyst I had understood that I would be able to call the savebutton function in a different m file using what voss had recommended no? I was then wondering if I would have to somehow save the savegraph button in some different way as well or if that wasn’t required
Fangjun Jiang
on 22 Jul 2024
SaveGraph() is defined as SaveGraph(hObject, handles). It is called only once inside SaveSpectrumPlotButton_Callback(hObject, eventdata, handles).
Making SaveGraph() a separte SaveGraph.m file, or keeping SaveGraph() inside the SaveSpectrumPlotButton_Callback.m file would make no difference for functionality.
I would choose the latter. As you are adding stuff to your existing working GUIDE application, less intrusive, less new files would be better.
Bera
on 22 Jul 2024
I see thank you Fangjun! I don’t want to close this question yet since I’m worried somethings may arise but during the meantime I appreciate everyone who answered my questions!
Bera
on 22 Jul 2024
@voss @Fangjun I just wanted to make sure since I haven’t used GUIDE before. Calling the savebuttonfunc is the same as the button being pressed right? As in I can call it multiple times to take a photo every time? Like would ai have to set a button value to 1 similar to app designer or would it work just by calling the function?
Voss
on 22 Jul 2024
You can just call the function. There is no need to set the button's Value.
Also, SaveGraph has to be a separate m-file function in order to be accessible outside the GUI.
Bera
on 22 Jul 2024
@voss oh does it. Everyone is saying different things, so I would have to make a new m file with it then right?
Fangjun Jiang
on 22 Jul 2024
A SaveSpectrumPlotButton_Callback.m has been created by copying the SaveSpectrumPlotButton_Callback(hObject, eventdata, handles) function from the original GUIDE .m file. My suggestion is to copy SaveGraph() function into the SaveSpectrumPlotButton_Callback.m file. SaveGraph() does not have to be a separate m-file in this context.
Voss
on 22 Jul 2024
That's right. Having SaveGraph as a local function in SaveSpectrumPlotButton_Callback works. I misunderstood what you were suggesting.
Bera
on 22 Jul 2024
Okay thank you so much guys! I was just handed this code myself so I don’t know it in too much depth, what handles would I give savegraph as a local function would it be the same or different ones then the original m file
Fangjun Jiang
on 22 Jul 2024
SaveGraph(hObject, handles) is called inside of the SaveSpectrumPlotButton_Callback(hObject, eventdata, handles) function. As long as the (hObject, eventdata, handles) is specified correctly for SaveSpectrumPlotButton_Callback(hObject, eventdata, handles), you don't need to do anything extra.
Image Analyst
on 23 Jul 2024
@Bera did you ever see my Answer below, in the official "Answers" section rather than up here in the "Comments" section?
Fangjun Jiang
on 23 Jul 2024
You will have to debug. It seems that the value of "count" is not a scalar on this line.
t.TasksToExecute = count
Fangjun Jiang
on 23 Jul 2024
Okay, that makes sense. You are writing code to mimic the effect of pushing a button on that GUI, so the necessary fields of that GUI should all provide valid input. Make sure you can do it manually first and then try your code.
More Answers (1)
Image Analyst
on 17 Jul 2024
I agree with @Steven Lord that you should just rethink and reorganize the workflow. That said, please read the FAQ for ideas on how to control one GUI from another:
especially the section that begins "Sharing between multiple GUIs."
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)