Accessing a function from GUI1 in GUI2

How can I call a function in GUI1 called Autoscaleimage from GUI2? (Created by GUIDE).
function [high,low]=Autoscaleimage(handles,Image,n)
mn=mean(Image(:));
sd=std2(Image(:));
low=mn-sd;
high=mn+n*sd;

3 件のコメント

Jason
Jason 2018 年 2 月 6 日
One thing that is stopping me is trying to write a seperate function that can extract an image of an axes belonging to a certain GUI.
I normally use getimage but this is for images on the sane GUI where it is being called from.
Adam
Adam 2018 年 2 月 7 日
Pass in the image handle directly from your GUI. It is always a good idea to keep hold of the handles of the things you plot, e.g.
hImage = imagesc( ... )
Jason
Jason 2018 年 2 月 7 日
Thanks

サインインしてコメントする。

 採用された回答

Jan
Jan 2018 年 2 月 6 日

0 投票

What does "a function in GUI1 called Autoscaleimage" exactly mean? Is this a subfunction in the M-file GUI1.m? Then it might be easy to move this function to an own M-file. Then it can be used from all other functions.
The input handles is not used inside this function. Then it would be cleaner to omit it.

8 件のコメント

Jason
Jason 2018 年 2 月 6 日
Hi, yes I get your point about no need for handles for this function, but I just used that as an example, how would I call the function is as above it had handles as an argument?
Jan
Jan 2018 年 2 月 6 日
@Jason: Then it matters, which handles this has to be. You have to explain more details. Please answer my question:
What does "a function in GUI1 called Autoscaleimage" exactly mean?
Functions are not "in GUI".
Jason
Jason 2018 年 2 月 6 日
編集済み: Jason 2018 年 2 月 6 日
Sorry...what I meant was I have a GUIDE GUI where the figure's tag is GUI1. I have a lot of buttons, checkboxes etc.
sometimes when I create functions in the GUIDE created m file i need to e.g. pass the state of a checkbox
val=get(handles.checkbox30,'Value')
and the function behaves differently for the 2 states. I also sometimes get a value from e.g. an edit box inside my function hence the need for handles.
My GUI is now very crowded (mainly with image analysis tools) and I have a push button which will call a 2nd GUI created in GUIDE. I want to use this 2ns GUI, (who's figure has a tag GUI2) to add some more image analysis tools to via push buttons on its figure (all done in GUIDE).
So I want to be able to reuse some of my functions in GUI1 on GUI2.
Stephen23
Stephen23 2018 年 2 月 6 日
編集済み: Stephen23 2018 年 2 月 6 日
"So I want to be able to reuse some of my functions in GUI1 on GUI2."
As Jan Simon wrote, move them to their own Mfiles and you can call them from anything you like.
Jason
Jason 2018 年 2 月 6 日
OK great, so there's not problem having a seperate mfile then with an argument of handles so I can access e.g. editbox contents?
Jan
Jan 2018 年 2 月 6 日
@Jason: Yes. Simply try it. You can access the GUI objects from anywhere, if you have their handles.
Adam
Adam 2018 年 2 月 6 日
You should never be wanting to pass the handles of one GUI to a standalone function when you can get away with it. I have done it on occasion, for specific purposes, when I wanted to create a system of button highlighting and enabling/disabling based on the state of an underlying program where passing all the ui controls to such a function would just be silly, but in general a handles structure should remain within its GUI, it has no business being outside it and can create all manner of bugs that you really don't want.
If you want the same functionality from two different GUIs you should extract the relevant parameters from the handles structures of one or other GUI so the common interface of the function needs to have no knowledge of a massive handles struct that comes with extraneous UI controls and goodness knows what else attached to it.
Jason
Jason 2018 年 2 月 6 日
OK thankyou for your comments - I get the message and will try to adapt my code as you suggest.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeInteractive Control and Callbacks についてさらに検索

タグ

質問済み:

2018 年 2 月 6 日

コメント済み:

2018 年 2 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by