Matlab gui with pause function - matlab

I am using the GUIDE for matlab gui.
The gui built in order to communicate with keithley current measurement device through GPIB.
When using a toggle button for Current measurement while loop, i am using a pause() function inside the while loop for each iteration and a ytranspose on the y array reading results.
function Measure_Callback(hObject, eventdata, handles)
global GPIB1
global filename
global timeStep
disp('Measurement in progress \n stopwatch starts!');
tic
x=0;
n=0;
while get(hObject,'Value')
fprintf(GPIB1, 'printnumber(smua.measure.i(smua.nvbuffer1))');
fprintf(GPIB1, 'printbuffer(1,1,nvbuffer1)');
A = fscanf(GPIB1);
if length(A)<20
x = x+1;
n = n+1;
t(n) = toc ;
y(x) = str2double(A);
plot(t,y,'-bo',...
'LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',10);
grid on
hold on
end
title('Current vs Time','FontSize', 15)
xlabel('Time [s]','FontSize', 15)
ylabel('Current [A]','FontSize', 15)
a = timeStep;
pause(a)
end
disp('Measurement terminated');
disp('Elapsed time: ');
elapsedtime = toc;
elapsedtime_string = num2str(elapsedtime);
disp(elapsedtime_string);
ytrans = transpose(y);
csvwrite(filename,ytrans);
fprintf(GPIB1, 'smua.source.output = smua.OUTPUT_OFF');
For the pause function i'm geting error:
?? Error using ==> pause Error while evaluating uicontrol Callback
For the transpose(y) function i'm also getting a error:
its undefined y.
Cant understand why are those errors and could use some help.
Thank you!

First off, as people say, post the errors and the code. Do you know if length(A) is smaller than 20 in the first time you run the loop? Because if not, A is not defined and you can't transpose something that is not there. Initialize A before the loop to something and see if the error persists (or print out length(A) to make sure the loop gets entered the first run).
As for the pause error, make sure pause is an int or double, not a string. If you get your global timeStep from the GUI field, it is probably a string and you need to covert it to double first.

Related

How can I check mouse events as a background process in Matlab?

I am using Matlab to develop an application which performs several mathematical operations, whose parameters can be changed when the mouse is clicked, as in the example below.
while time<endtime
calculate_manythings;
if ~mod(time,checkmouse)
mouseinput_timeout(timemouse, gca);
change_calculation_parameters;
end
time=time+1;
end
At the moment, I am pausing the operations periodically to check for mouse events, but this is slow and unpractical. How can I monitor these continually and run the code at the same time? Could I make the mouse event checks a background process using parfeval, for example?
Many thanks,
Marta
you can use callback functions. here I used 'ButtonDownFcn':
timeinterval = 1; % seconds between mouse clicks
% generate axes with callback function
h = plot(rand(1,2),'LineWidth',6);
set(gca,'ButtonDownFcn',#callback);
% reset Tag and time
h.Tag = '';
tic;
while true
drawnow;
if strcmp(h.Tag,'Pressed') % if pressed
t = toc; % check time passed
if t >= timeinterval
% change parameters
disp('Pressed');
h.Color = rand(1,3);
% reset timer
tic;
end
% reset Tag
h.Tag = '';
end
end
and the callback function is:
function callback(src,~)
src.Children(1).Tag = 'Pressed';
end

Saving a specific simulation time

I have a two-track model implemented in simulink. To control the velocity I use a PID-controller, so that the the output of the velocity looks like this:
now I want to implement a MATLAB function or simulink block that tracks the time when the velocity reaches a steady-state-behaviour and puts it into some kind of storage. I tried to implement something like this via the following MATLAB function with MATLAB-function-block:
function y = fcn(t,v,dv,tv)
%#codegen
if (v==tv+0.01) & (dv<0)
y=t
end
t is the clock-signal, v the velocity, dv the first derivation of the velocity and tv is the targetvelocity. The problem about this function is that there "y is not defined on some execution paths". do you have any ideas how to make this work?
I solved the problem without a MATLAB function using the simulink blocks data store memory and its read & write blocks. The signal that is coming in from bottom right side is the momentary velocity. The if statement is
(u1 >= 22.2) & (u1<=22.3) & (u2<0)
Since simulink is using time steps and the momentary velocity will never be exactly 22.2, you can not use u1==22.2
In SimuLink add a second output and a fifth input to your function. Then use that new output as a feedback to the function.
function [y, output] = fcn(t,v,dv,tv,input)
y = 0;
output = input;
if (v == tv + 0.01) && (dv < 0)
y = t;
if (input == -1)
output = t;
end
end
Attach the output to an IC block where you set the input initial value to -1 or whatever value you want to use. Then attach the IC block to the input of the function. output will be feedback constantly through the function. Once it's set it will reatin it's value forever.
function y = fcn(t,v,dv,tv)
%#codegen
y = zeros(length(t),1); % Initialise the array
for ii = 1:length(t)
if (v==tv+0.01) & (dv<0)
y(ii)=t;
else
y(ii)=0;
end
end
y(y==0)=[];
end
Two changes: added a semicolon after y=t to force it to not print it every time it is set. Second, your question, else y=[];, which means that y will be an empty matrix if you do not adhere to your if statement.
It now stores a 0 each time you do not adhere to the if statement. The line y(y==0)=[]; deletes all zeros, comment this line if you want your y to be the same length as the input variables.
function y = fcn(t,v,dv,tv)
%#codegen
y = zeros(length(t),1); % Initialise the array
ii=1;
while exist(t)
if (v==tv+0.01) & (dv<0)
y(ii)=t;
else
y(ii)=0;
end
ii = ii+1;
end
y(y==0)=[];
end

Matlab assignin('base',...) Resets

I am attempting to write a function that, if called before any animation, will handle the close events without extra code in the animation file.
function matlabStopFunction(varargin)
persistent runs
if runs==2
runs = [];
end
if isempty(runs)
evalin('base','figure(''DeleteFcn'',#matlabStopFunction);');
runs = 1;
else
assignin('base','play',false);
pause(1);
runs = 2;
end
end
Here's a sample animation code that I've been using:
function sampleAnimation
matlabStopFunction;
r = 5;
th = 0;
play = true;
while play
x = r * cosd(th);
y = r * -sind(th);
plot(x,y,'r*');
axis([-10 10 -10 10]);
th = th + 45;
pause(0.25);
end
end
The stop function works fine creating the figure, and when I close the figure, it calls the same function as expected (including the assignin on line 10). However, when stepping through, when I first get back to the base function (sampleAnimation), play is false as would be expected:
But when I step one more line, play is reset to true
Am I incorrectly assigning the value of play to false in the stop function, and if so, how could I correct this so that the animation stops when the figure is closed while keeping the code inside the animation to a minimum? I am trying to replicate the method on this blog except with all the code contained in a separate file.
I am running Matlab 2014b on Windows 8.1.
To answer your question - you are modifying the value play in the base workspace - where as the loop is in the workspace of the sampleAnimation function -> so you are not changing the required value to stop the animation. To verify this clear your variables in the base workspace clear before you run your code and you will see that the variable play is created and set to false.
By the way there is a much simpler way to do this, you animation can create a figure and then you can stop when it is deleted:
function sampleAnimation
h = figure;
r = 5;
th = 0;
while ishandle ( h )
x = r * cosd(th);
y = r * -sind(th);
plot(x,y,'r*');
axis([-10 10 -10 10]);
th = th + 45;
pause(0.25);
end
end

how to abort matlab function in runtime with code?

I have a matlab code running in a loop. The code is pretty heavy and time-consuming. Instead of using Ctrl-C, I am looking for a way to abort the function with GUI callback in runtime. I have my code designed as follows
function test
figure;
uicontrol('pos',[20 20 40 20],'string','abort','fontsize',12, 'callback', 'error(''p'');');
k=0;
while(k<10000)
m=1:10000;
x = rand(size(m));
for t=1:10000
x=x+sin(2*pi*m*0.02 + mod(t, 5)*pi);
end
% other code will be run here
plot(m, x);
drawnow;
k=k+1
end
end
Above code just as an example. I know it could be optimized but I don't concern about that now. I just want to know why above code doesn't work. The callback in which the 'error' function issued will not abort the code. How to make it work? Thanks.
The error callback is not called within the loop. Hence, the error callback and the loop are independent processes.
You could design your function this way:
function test
figure;
AbortButton = uicontrol('Style','togglebutton','pos',[20 20 40 20],'string','abort','fontsize',12);%, 'callback', 'error(''p'');');
k=0;
figure;
while(k<10000)
val = get(AbortButton,'val');
if (val == 0)
m=1:10000;
x = rand(size(m));
for t=1:10000
x=x+sin(2*pi*m*0.02 + mod(t, 5)*pi);
end
% other code will be run here
plot(m, x);
drawnow;
k=k+1;
elseif (val == 1)
break;
end
end
end
This is a rudimentary way, but might help.

using timer in MATLAB to extract the system time

!I am using MATLAB to design an Analog Clock. Currently, my code simply displays the (or plots rather) the clock design with the hands (hours, mins, secs) and does not tick. Here is my code:
function raviClock(h,m,s)
drawClockFace;
%TIMER begins-------
t = timer;
t.ExecutionMode = 'FixedSpacing'; %Use one of the repeating modes
t.Period = 1; %Fire on 1 second intervals
t.TimerFcn = #timer_setup; %When fired, call this function
start(t);
set(gcf,'DeleteFcn',#(~,~)stop(t));
end
function timer_setup(varargin)
format shortg;
timenow = clock;
h = timenow(4);
m = timenow(5);
s = timenow(6);
% hour hand
hours= h + m/60 + s/3600;
hourAngle= 90 - hours*(360/12);
% compute coordinates for pointing end of hour hand and draw it
[xhour, yhour]= polar2xy(0.6, hourAngle);
plot([0 xhour], [0 yhour], 'k-','linewidth',7.4)
% minute hand
mins= m + s/60;
minsAngle= 90 - mins*(360/60);
% compute coordinates for pointing end of minute hand and draw it
[xmins, ymins]= polar2xy(0.75, minsAngle);
plot([0 xmins], [0 ymins], 'r-','linewidth',4)
%second's hand
second = s;
secAngle = 90- second*(360/60);
[xsec, ysec]= polar2xy(0.85, secAngle);
plot([0 xsec], [0 ysec], 'm:','linewidth',2)
%end % while ends
end
%--------------------------------------------------------
function drawClockFace
%close all
axis([-1.2 1.2 -1.2 1.2])
axis square equal
hold on
theta= 0;
for k= 0:59
[xX,yY]= polar2xy(1.05,theta);
plot(xX,yY,'k*')
[x,y]= polar2xy(0.9,theta);
if ( mod(k,5)==0 ) % hour mark
plot(x,y,'<')
else % minute mark
plot(x,y,'r*')
end
theta= theta + 360/60;
end
end
%-----------------------------------------------------------------
function [x, y] = polar2xy(r,theta)
rads= theta*pi/180;
x= r*cos(rads);
y= r*sin(rads);
end
This is simply taking a static data of values for the HOUR, MINUTE and SECOND arguments when i initially call my function. I tried using the following in a while loop but it didn't help much
format shortg
c=clock
clockData = fix(c)
h = clockData(4)
m = clockData(5)
s = clockData(6)
and passing the h, m and s to the respective cuntions. I want to know how I can use the TIMER obkjects and callbacks for extracting the information of [hrs mins secs] so i can compute the respective point co-ordinates in real time as the clock ticks.
I'd do a couple of things here.
First, you probably don't really need to pass the h, m, s inputs, if you are displaying current time. Add this to the top of your function to auto set these variables.
if nargin == 0
[~,~,~,h,m,s] = datevec(now);
end
Then, it is pretty easy to use a time to call this function periodically. Something like this.
t = timer;
t.ExecutionMode = 'FixedSpacing'; %Use one of the repeating modes
t.Period = 1; %Fire on 1 second intervals
t.TimerFcn = #(~,~)raviClock; %When fired, call this function (ignoring 2 inputs)
start(t); %GO!
Use docsearch timer for in depth documentation of the timer objects. But the code above should get you started.
To stop the timer, run
stop(t);
To stop the timer when the window is closed, put the stop command into the window deletion callback:
set(gcf,'DeleteFcn',#(~,~)stop(t)); %NOte: Better to explicitly use a figure number, rather than gcf.