why would Matlab skip the loop? - matlab

I have a question that I can seem to wrap my head around,
I have created a gui that uses the unput from the user to run a script within the gui and later display the results to the user interface. What I dont understand is that everything is running ok until I choose one particular file. The interesting and mind boggling is that the script jumps a nested loop (whie this dosent happen for the other choices). I dont understand why this happens and cant find anything relavent on the internet. The script stand alone executes perfectly.
for i=2:length(Data_last_values)
for j=2:length(temp_nov)
if Data_last_values(i,4)>temp_nov(j,1) & Data_last_values(i,4)<temp_nov(j-1,1)
Data_last_values(i,9)=Data_last_values(i,3)+Q_leak_dot(j);
disp('gone through loop')
end
end
end
displayed above is the part of the nested loop that matlab chooses to skip. would any one know why this happens? below I present the gui script:
if not(isempty(cell2mat(z)))
run Daughter
assignin('base','avg',data(:,8))
axes(handles.axes1);
markerSize = 50;
scatter(Data_last_values(:,4),Data_last_values(:,9),markerSize,Data_last_values(:,5)) % -> volumetric mass flow is used in the color bar
colormap(jet(20));
h=colorbar;
xlabel('T_{water}[C]')
ylabel('Q_{out} [W]')
ylabel(h, 'Volumetric Flow [Lt/min]')
grid minor
myvar = evalin('base', 'avg');
y=line([0 max(Data_last_values(:,4))], [mean(myvar) mean(myvar)]);
xlim([0 max(Data_last_values(:,4))])

Related

Matlab Get Handle for Layout

As the title states, I need to get a handle for my Matlab application. My class is derived from matlab.apps.AppBase and is app.UIFigure (if that matter, I'm still learning Matlab). My main goal is to change the mouse cursor to watch after a button is clicked and data is processed in the background.
I have tried:
set(gcf,'Pointer','watch')
But gcf is just empty, so it creates a new figure. I have also gotten all of the figures, using:
figs = findall(groot,'Type','Figure')
Which finds all of the figures I am using. I believe that I need to get the overall application figure and find the handle, but I am unsure how to do that.
There is no pointer property for uifigure; otherwise, you would be able to use app.UIFigure.Pointer = 'watch' as suggested by #CrisLuengo.
However, specially for uifigure MATLAB provides a nice looking and powerful progress bar uiprogressdlg. You can make it indeterminate with uiprogressdlg.Indeterminate = on;. I find this working pleasingly well.
Here is an example:
f=uifigure;
progressdlg=uiprogressdlg(f,'Title','Progress','Message', 'Doing something please wait', 'Indeterminate','on');
pause(10); % Run your algorithm.
% Delete the progress bar after work done.
progressdlg.delete();

how to save figure of gui plot which will run on a computer that does not have matlab

I have made a Gui program that I compile to EXE application which lots csv file into graph data. I buit a save button but I do not know how save figure with different name each time cause (savefig anduisave both uses matlab program). I am posting my code below if anyoff you guys figure out how to save gui figue into image or anything that does require matlab to open. Last function is the callback function for save button.
function ma_Callback(hObject, eventdata, handles)
% i tried uisave but not possible to run computer without matlab cause mcr
% does not run uisave
% i tried copyopbj but since i did not put a name on my figure it did not
% work
%savefig
If you are trying to save a kind of image from your figure, then the best option which supports many aspects, is using print function.
I have already done this in a compiled app and it works perfect. Using print function you can set different file type(vector formats like *.svg are also supported), resolution(dpi), and so many others.
Although you can use print function directly on the figure, but I found that the best way (More Customization like removing or adding some objects and changing many options) is to follow this steps:
Create another figure with Visible='on' but a position that is out of screen with the same width and height of Main Figure.
normalized position = [-1, -1, ?, ?]. (Create this figure in start up and don't destroy it until your app exits, let call it Print Figure).
Copy your figure content (or the parts you are interested in) using copyobj to that figure (you may need to set Parent property of some key objects like panels to this new figure). They would look exactly as they look in the main figure, because they have the same properties. you can add or remove some objects in this step.
Change aspect ratio of "Print Figure" for a better output.
Print this figure with all options (file format, dpi, ...) you need. in my own GUI i allowed the user to change this settings with an input dialog.
In my app i used functions like winopen to show the output, and output directory to the user, when the print task was done. Print process takes some time, specially if dpi is huge, so it is also a good idea to inactivate buttons and show wait cursor.
Update:
a simple usage would be:
print(MainFigure, 'myFileName', '-dpng', '-r300')

MATLAB Slider GUI with Background While Loop to Control Feedback Servo Motors

I am attempting to implement a servo motor with feedback control program in MATLAB for multiple servo motors. The objective is to read servo(s) analog output, compare with a user value from slider bar(s), write the new position to the servo, and continue to write or monitor analog output to ensure servo maintains last user value. I am having trouble because I cannot figure out the optimal flow to always keep the GUI open and accepting user values while a while loop runs in the background. Below is the general structure that I would like the code to output:
test_gui %calls GUI figure with slider bar
GUI window with Slider bar pops up
%Callback for when user slides bar
function slider(i)_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
load('user.mat'); %Load previous user input
sze=size(user,1);
user(sze+1,1)=get(hObject,'Value'); %Add new user input to previous input
save('user.mat','user') %Save variable to be accessed by outer loop
'user' is passed into a continuous while loop that writes servo position
while user ~= 'c' %Continue to run until close window callback
load('user.mat'); %load user input from callback
Write_Servoi(a,user(end,:),add,speed,pinin,myServo) %Writes to servo(s) based on last input, allows servo(s) to maintain position if torqued out of position
end
During this while loop, I would like the user to be able to continuously change the slider and send this slider value into the while loop to be written to the servo. However, I cannot figure out how to update user when in the while loop.
If anyone has any ideas on how to get this to work, please let me know. I would greatly appreciate any help. I am open to changing the structure, as long as the objectives above are satisfied.
Robert
To update the user variable, you'll need to make sure it is properly loaded. Calling load with no output does not always load your variable into the function workspace.
Instead of load('user.mat');, you'll need to load your file as a structure and then retrieve your variable from the structure. To accomplish this, do the following:
s=load('user.mat');
user=s.user;

Changing psychtoolbox screen

I am using Psychtoolbox to display on a screen as part of an interface. I create a blank screen with [win,Rect]=Screen('OpenWIndow',etc.etc...) in my main script. I would like to use a function call later on in the script to update the screen, but I have no idea how to get the values in and out of the function to change the Screen.
So something like:
%main script
[win,Rect]=Screen('OpenWIndow',0,[0 0 0],[0 0 1280 1024]);
%do other processing (in my case communicating with another computer.)
DrawStuff() %function that takes variables from script and updates the screen
% continue script
Screen('Flip',win) is what you are looking for, but you need to issue some drawing commands for it to show anything.
Please read the file PsychDocumentation/PTBTutorial-ECVP2013.pdf in the psychtoolbox distribution to understand the basics of stimulus presentation.

Conditional pausing in Matlab (not Debugging)

I am new to Matlab so please bear with me.
So I created a two GUID GUI in which one generates a dynamic data and updates the plotting every second so I used pause(1) (which is continuous) and the second one takes the full data of the first GUI and plots it (opens on button press)
Is there a way where if I open the the second GUI the first GUI pauses and if and only if the second GUI is stopped the first GUI resumes its process?
Thanks in advance.
Update
Example:
gui1.m
function guie1()
for ii=1:100
c = magic(ii)
plot(c);
% a button at this point
% Some pause condition
drawnow;
end
so when I click on that button it would open a window (a new figure may be) so unless I close is the loop should be paused.
Here is the example:
run(fullfile(docroot,'techdoc','creating_guis','examples','callback_interrupt'));
Here is the link:
http://www.mathworks.com/help/matlab/creating_guis/callback-sequencing-and-interruption.html
Update:
Here:
http://blogs.mathworks.com/videos/2010/12/03/how-to-loop-until-a-button-is-pushed-in-matlab/
http://www.mathworks.com/matlabcentral/fileexchange/29618-spspj