Use both GUI and script at the same time - matlab

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.

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')

How can I stop matlab pde solver in pde tool box when solution is unstable

I am solving two coupled time dependent PDE eqations using pde tool box.The simulation box size is x=6 and y=10.Currently I solve it properly and acces the solution data using
results = solvepde(model,tlist); u = results.NodalSolution(:,1,:);
What I need to do is now to stop the code when the solution u is unstable along the y axis.That means I want to monitor solution of u along y axis while running the code and stop it when it meets a criteria that I want.(For example I want to stop the code when solution of u along y equals to 0)..How can I do it using pde tool box?
Here is what I tried so far.Is there easy way to access the results while code is running?.That is I want to access the solution for each time step.
%calculate solutions
n=4000;
tlist = linspace(0,200,n);
partial=zeros(49,1);
for i=1:n
results = solvepde(model,tlist(i:i+1));
u = results.NodalSolution(:,1,1);
v=results.NodalSolution(:,2,1);
u1=results.NodalSolution(113:161,1,1);
u2=results.NodalSolution(1,1,1);
u3=results.NodalSolution(4,1,1);
for j=1:49
partial(j)=u1(j)-0.5*u2-0.5*u3;
end
sigma=sum(partial);
if sigma>1e-4
disp('verified')
return
end
end
There are several ways to stop a code during execution:
keyboard
Using this function stops the execution and gives you access to the keyboard, such that you can see all values.
warning('Solution is unstable')
This won't actually stop the code but will give you a warning stating that the solution is unstable.
error('Solution is unstable')
This will actually stop the code, giving you the error, that the solution is unstable.
break
This will not give you any feedback, but it will stop the for-loop and then continue the rest of the code.
In all cases you include the way of stopping the code in an if-statement, like
if (all(u==0))
*statement from above*
end

Save Figure generated from HeatMap() from Bioinformatics Library - Matlab

I would like to save the image I generate the HeatMap() function from the bioinformatics library.
I cannot figure out to have the image without manually exporting the data.
I'd prefer to use HeatMap over Imagesc because it automatically scales the data to the mean.
I do not have the Bioinformatics Library for matlab, However you should be able to get the current figure with the function gcf. It is not the most stable way, but in most cases this will work excellent. Then you can save the figure as a figure or image with saveas.
HeatMap(...);
hFig = gcf;
saveas(hFig,'myName','png'); % You can use other image types as well and also `'fig'`
There is a second way as well. The HeatMap class also contains a plot method. This method may return the figure handle to the HeatMap plot then save the image with saveas.:
hHM = HeatMap(...);
hFig = plot(hHM);
saveas(hFig,'myName','png');
I share a part of a Matlab code that I used when I analyzed some data from TCGA
#I was considering two gene expression level taken from cancer disead tissue and
# healthy tissues comparing them with this Heatmap
labela=Gene;
labelb=Gene2;
data1=[dataN(indg,:) dataC(indg,:); dataN(indg2,:) dataC(indg2,:);];
H=HeatMap(data1,'RowLabels',
{labela,labelb},'Standardize','row','Symmetric','true','DisplayRange',2);
addTitle(H,strcat('HeatMap',project));
addXLabel(H,'patients');
addYLabel(H,'geni');
#I have allocated the plot in a variable
fig=plot(H);
# Then I saved the plot
saveas(fig,strcat('D:\Bioinformatica\Tesina...
Prova2\Risultati_lihc\',dirname,'\HM.fig'));
But if you have to run one single object I advise against waisting your time in writing code (even if it is always a good thing to explore and strengthen your knowledge), I used because each time I was running a pipeline. Otherwise you can go on
1)File ----> Export Setup
2) Export
3)Save in the format you do want!

How to forward test and plot?

Have received a lot of help. Will look for some more. Learning a lot by just going through the code.
I have thus far:
Loaded an xlsx file using xlsread function.
After this calculated the correlation coeff. Assigned it a variable(?).
Then used(Thanks #Robert P.):
BUYx = x(find(APC < -2.9079,1,'first'))
SELLy = y(find(APC < -2.9079,1,'first'))
BUYy = y(find(APC > 0.44,1,'first'))
SELLx = x(find(APC > 0.44,1,'first'))
To get the first value meeting a particular condition.
Now I want to forward test from where the first value is received till another condition is met. At the same time I would like to make a plot the results.
I am just wondering where to start looking to code/try this.
Please point me in the direction of the functions required. I think it has to loop through all the elements after receiving entry. Not sure which is the right way to go about it.
Thanks
Context:
I am trying to test a StatsArbitrage strategy. I was using excel for the work but I can't seem to get the right functionality.
Basically, when we have an entry(any set of the BUY SELL) I want the system to automatically start calculating Profit/Loss from the next data point and give an output to a new variable. Then I would plot it.
Then search for new entry and again start Profit/Loss calculation.

Get figure handle from different mfile and plot in GUI

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)