Get figure handle from different mfile and plot in GUI - matlab

I am tasked to design a GUI and I need to use variables and plots from a different mfile which I created earlier. I'm pretty confident about getting variables from the processing mfile but I'm not sure how to get the plots/figures.
So basically my question is whether I can get() a figure from my mfile and then set() an axes to that figure inside my GUI.
Note: The reason I am doing this is because I want to keep the processing of data separate from the GUI mfile. I could just dump all the processing in the callback of my process button but that's not good. I would also appreciate good coding practices for my case since I have never worked with GUI's before (only scripting with PHP and MATLAB)
Note2 (rundown of what has to be done): In the GUI we basically are supposed to load 2 files, we then press the "process" button and then 4 plots have to appear. All the processing code already exists in a previously written mfile (by me).
Thanks! :)

I figured it out myself! What I did was use gcf to get the current figure like so: output.worldmap = gcf I then passed the object back like so: setappdata(0,'output',output) and grabbed it again inside my callback function like so: getappdata(0,'output') and used the following function to set the axes set(output.worldmap,'CurrentAxes',handles.axes_worldmap) I also made sure that the correct axis was set before I actually ran my mfile which does the processing with axes(handles.worldmap)

Related

How to control the figure which appears in bayesopt function?

bayesopt draws figures like this:
How to access such figures in order to modify title or something? If I use gcf it is not guaranteed I get the correct figure because I could change focus to another figure window during execution.
Apparently bayesopt does not allow you to return a figure handle. So I suggest that on the line directly after your call to bayesopt you call h=gcf;, thus forcing your program to return the figure handle to h, which can then be modified at any desired time, even when moving to other figures.
results = bayesopt(fun,vars,Name,Value); % execute bayesian optimisation
h = gcf; % directly after optimisation grab a figure handle
Now you can modify properties in h, e.g. for the title you'd simply do
h.CurrentAxes.Title.String = 'Your plot title'
The reason this works is that MATLAB does not display figures until the full code has finished running. At least that's the case for my script, where I generate a figure, perform a few minutes of optimisation, then generate another figure. Both figures get displayed at the same time, i.e. when MATLAB has finished running the full program. There's thus no way you can click other figures when the code is running, since they are simply not there. If you happen to have older figures open (from other scripts), focus gets shifted to the latest created figure anyway, the moment it's created in the code (so not when it's displayed), thus you'd need to click a figure in the few milliseconds between the bayesopt call finished and the gcf call, which I'd say is so improbable that it's not worth considering, especially since it requires manual intervention.
As was pointed out in comments by Cris Luengo and Dev-iL, the figures are tagged and can thus be found using findobj:
h1 = findobj(0,'tag','bayesopt.MinObjective')

Wait for cftool to close, then continue execution

I'd like to call the cftool from a function/script, do my fitting, store the variables to the workspace and then resume execution of my function. Naively, one could assume that assigning a handle to the GUI and then using waitfor(), like for a graphics object, would do the job, but that was a bit short-sighted.
Minimally this reads
h = cftool;
waitfor(h);
disp('happy fitting');
and of course does not work.
Cheers.
The cftool does not return a handle, so you need to search for it using findall:
cftool
f = findall(0,'Type','Figure');
waitfor(f(1))
disp('Happy fitting!')
If you have multiple figures f may have multiple values and it could cause some issues. So keep track of your figures and check which position has the gui handle, before you call waitfor.

Use both GUI and script at the same time

I have a .m file (script) that is controlling a real-time robot.
What i do within this file is:
1- find a trajectory
2-infinite loop:
read from robot
update robot
plot some stuff (basically I'm drawing a new point in each iteration that represents the position of the robot in a previously opened map, it's updating the map)
end of loop
What I want to do is to create a GUI that allows me to make the plots and see some values that the robot returns at the same time, in real time.
From what I read, MATLAB can't run both a script and a GUI at the same time.. I can make it plot in real time in the GUI but I can't seem to be able to update the values returned by the robot in text boxes in GUI..
Do I have to put it all in the same file or is there a way for the GUI and the script to work in separate files?
Thank you in advance!
MATLAB has not trouble running both. I don't know where you read that but it's not true; MATLAB is not the best tool to solve this problem, but it can do it.
First, I'm going to frame your problem in code, to make it easier to solve. Your question is vague and general, so my response has to be be general as well. I'm making some assumption about your function structure, but it really should look a lot like this:
endflag = 0;
while ~endflag
robotData = getRobotData(robotHandel);
derivedData = doStuffWithData(robotData);
updateRobot(derivedData);
showData(robotData, derivedData)
endflag = checkEndFlag(robotData, derivedData)
end
So, your problem is the showData functionality. What it should do, is determine which values need to be displayed from it's inputs, and pass those to your GUI. Like so:
function showData(robotData, derivedData)
guiInputData = dataParser(robotData, derivedData)
YourGUIFunctionName(guiInputData)
end
The GUI function should then build itself using those inputs. Any GUI function working that way, will do what you want it to. If you want a more specific solution, you need to give me more specific information about your problem. Good luck, I hope this helps.

Pass data between gui's matlab

I have two gui's one is main gui and other is sub gui. In opening function of main gui i used open('subgui.fig'); to open sub gui. Main consists of 5 edit box and one pushbutton. After pressing pushbutton the data in those 5 edit boxes should be passed to sub gui and main gui should close. Please any one help me to do this.
Let us take a simple case of one editbox and one pusbutton in main GUI and one editbox in sub GUI that will get value from the editbox in main GUI. One can easily extend this to as many editboxes as needed. The basic medium of data storage and retrieval would be a global structure data1.
For the sake of understanding the codes, let us take the following assumptions -
Main GUI is named as main_gui.m and thus has an associated
main_gui.fig from GUIDE. Main GUI's figure has the tag main_gui_figure.
Sub GUI is named as sub_gui.m and thus has an associated sub_gui.fig
from GUIDE.
Edits to be made in main_gui.m
Inside editbox's callback, add this -
global data1;
%%// Field in data1 to store the string in editbox from main GUI
data1.main_gui.edit1val = get(hObject,'String');
Inside the pushbutton's callback, add this right before it's return -
global data1;
sub_gui;
delete(handles.main_gui_figure);
Edits to be made in sub_gui.m
Inside sub_gui_OpeningFcn, add this -
global data1;
set(handles.edit1,'String',data1.main_gui.edit1val);%%// Tag of editbox in sub-gui is edit1
Hope this works out for you! Let us know!
There are probably more than one ways to achieve this. But one of the approaches is to define a function that takes two input arguments: 1) handles to the destination figure and 2) whatever data from the source figure.
The following psuedo code doesn't necessarily run in MATLAB, but it gives the basic idea:
function takeAction(uihdls, data)
set(0, 'CurrentFigure', uihdls.fig); % uihdls.fig is the handle of the destination figure.
set(gcf, 'CurrentAxes', uihdls.aexs1); % axes1 is inside fig
plot(data.x, data.y); % Do some plotting
set(uihdls.editBox, 'String', data.string); % Modify some property of a control inside fig.
key_Callback(uihdls.fig, data.keyData); % Call a callback function of the destination figure
return
This function can be called by the source figure whenever it is ready to do so.
A bit more work - but I think it's worth it.
I usually use the MVC pattern for that. Practically it means to write a controller object that will pass the messages through to the required fields.

How to find call an external function from a GUI in MATLAB?

I have an external function (myfun.m) which will generate 4 images. I want to use a textbox in a GUI to give the function the input parameters (e.g. the name of the original picture), but I have no clue how I can run myfun.m in my GUI. PLease help
First, I would recommend having a different textbox for each input to your function. For example if your function asks for 4 inputs, I would have four textboxes. This will avoid pain on the user's end to format their text input a certain way, as well as pain on your end on parsing a long text string into several inputs.
Second, if your function is within MATLAB's path, then you can call that directly from your GUI no problem just as you would any of MATLAB's built-in functions. You probably want to make a push-button that has a callback to execute that functions.
The way the callback for this function should look is to l