現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
GUIDE code from pre 2015 need to access the button press function and "programatically press the button" from a different m file
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
Steven Lord
2024 年 7 月 17 日
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
2024 年 7 月 17 日
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
採用された回答
Fangjun Jiang
2024 年 7 月 17 日
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 件のコメント
Bera
2024 年 7 月 17 日
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
2024 年 7 月 17 日
@Fangjun Jiang Like doesn't it need a bunch of external properties to behave as expected?
Bera
2024 年 7 月 17 日
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
2024 年 7 月 18 日
編集済み: Fangjun Jiang
2024 年 7 月 18 日
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
2024 年 7 月 18 日
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
2024 年 7 月 18 日
@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
2024 年 7 月 18 日
編集済み: Voss
2024 年 7 月 18 日
@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
2024 年 7 月 19 日
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
2024 年 7 月 19 日
Fangjun Jiang
2024 年 7 月 19 日
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
2024 年 7 月 19 日
@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
2024 年 7 月 21 日
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
2024 年 7 月 21 日
@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
2024 年 7 月 22 日
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
2024 年 7 月 22 日
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
2024 年 7 月 22 日
@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
2024 年 7 月 22 日
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
2024 年 7 月 22 日
@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
2024 年 7 月 22 日
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
2024 年 7 月 22 日
That's right. Having SaveGraph as a local function in SaveSpectrumPlotButton_Callback works. I misunderstood what you were suggesting.
Bera
2024 年 7 月 22 日
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
2024 年 7 月 22 日
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
2024 年 7 月 23 日
@Bera did you ever see my Answer below, in the official "Answers" section rather than up here in the "Comments" section?
Fangjun Jiang
2024 年 7 月 23 日
You will have to debug. It seems that the value of "count" is not a scalar on this line.
t.TasksToExecute = count
Fangjun Jiang
2024 年 7 月 23 日
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.
その他の回答 (1 件)
Image Analyst
2024 年 7 月 17 日
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."
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)