How to create a textbox with a changing variable?

17 visualizaciones (últimos 30 días)
AbdullahAJ6
AbdullahAJ6 el 8 de Jul. de 2018
Respondida: AbdullahAJ6 el 14 de Jul. de 2018
I have a code which runs a movie using drawnow (not my own code, just adding stuff to it) and I would like to add a text box which indicates the frame number as the movie is playing.
I can get it to work with the 'annotation' function and using num2str but it seems like the numbers are overwritten on top of each other like in the file.
Curiously, when I try it using the title:
title(['i = ' num2str(i)])
it works fine
  3 comentarios
Robert U
Robert U el 9 de Jul. de 2018
Hi AbdullahAJ6:
It looks like you are creating new annotations every loop iteration. If so, create an annnotation object of your annotation and modify the "String" property.
Kind regards,
Robert
AbdullahAJ6
AbdullahAJ6 el 14 de Jul. de 2018
Editada: AbdullahAJ6 el 14 de Jul. de 2018
@Robert U
Hello, Thank you for your help, but do you mind elaborating? I read the document but I am unsure of what you mean by annotation object.
Here is my code, if it helps:
clear
% Select folder containing the data files
folder_name = uigetdir
cd(folder_name);
load('Phase_Data.mat')
%hold on
%Detect Size of Phase_Data file
Mat_Size = size(Phase_Data)
Time_Steps = Mat_Size(1,1)
No_Of_Spheres = Mat_Size(1,2)/3
%Force_Amp = 1*10^(-6);
View_What = 1;
% 1 = View Position
% 2 = View Velocity
% 3 = View Acceleration
% Next three parameters to be set if viewing to be slowed down
SlowMotion = 0;
% 0 = Regular Speed
% 1 = View in Slow Motion
PauseTime=0.05; % Pause in seconds before next frame
% Next two parameters to be set if viewing to be sped up
FastForward = 1;
% 0 = Regular Speed
% 1 = Speed up View
% Factor by which viewing is faster. Only relevant if FastForward= 1
SpeedFac = 3;
% Just checking that FastForward and SlowMotion not both turned on
if (SlowMotion == 1) && (FastForward == 1)
display(' ')
display('Program excecution to be terminated by m-file...')
error('WARNING: FastForward = 1 and SlowMotion =1 is incosnsistent')
end
% This needs to be set to any value 1, 2,3,...
% otherwise 'drawnow F(j) = getframe;'
% below does not work. It is not good to use the For-variable i
% in Figure(...) statement below since this then starts building up
% thousands of figures in memory and computer freezes at some point.
% If the same value j=1 is used all the time then only one frame
% is generated and deleted and updated for every i-value.
% When saving things as a video I may have to use i instead of a
% constant value j=1 or j=2 etc...
j=1;
if View_What == 1
%Define Start and End column where position data are in matrix
Read_Start = 1;
Read_End = No_Of_Spheres;
%Create matrix with all position values
Pos_Check_Mat = Phase_Data(:,Read_Start:Read_End);
%Find the maximum of all maxima
All_Max_Mat = max(Pos_Check_Mat);
%Set plotting limit to this maximum of maxima
Pos_Max =max(All_Max_Mat);
end
if View_What == 2
%Define Start and End column where velocity data are in matrix
Read_Start = No_Of_Spheres+1;
Read_End = 2*No_Of_Spheres;
%Create matrix with all velocity values
Vel_Check_Mat = Phase_Data(:,Read_Start:Read_End);
% Find the maximum of all maxima
All_Max_Mat = max(Vel_Check_Mat);
% Set plotting limit to this maximum of maxima
Vel_Max =max(All_Max_Mat);
end
if View_What == 3
%Define Start and End column where velocity data are in matrix
Read_Start = (2*No_Of_Spheres)+1;
Read_End = 3*No_Of_Spheres;
%Create matrix with all velocity values
Acc_Check_Mat = Phase_Data(:,Read_Start:Read_End);
% Find the maximum of all maxima
All_Max_Mat = max(Acc_Check_Mat);
% Set plotting limit to this maximum of maxima
Acc_Max =max(All_Max_Mat);
end
% This is required for movie; found it ob Matlab site
% after much suffering.
loops = Time_Steps; %Abs: This seems to be pointless
F(Time_Steps) = struct('cdata',[],'colormap',[]);
% These commands merely generate a line at zero which
% is superposed on plots later
% x0=linspace(0,No_Of_Spheres+1,No_Of_Spheres);
x0=linspace(0,No_Of_Spheres+1);
sz=size(x0);
y0=zeros(sz);
count=0;
for i=1:1:Time_Steps
% START : SHOW ALL AT REGULAR SPEEED OR SLOW MOTION
if FastForward == 0
% The actual data points to plot are defined
x=linspace(1,No_Of_Spheres,No_Of_Spheres);
y = Phase_Data(i,Read_Start:Read_End);
% Now we interpolate spline between these data points to enable
% displaying a smooth curve
xx = 1:0.1:No_Of_Spheres;
yy = spline(x,y,xx);
% Begin plotting
hold off
% Plot original data points
plot(x,y,'b*')
xlim([0 No_Of_Spheres+1])
% Determine range limits for y-axis and set labels
if View_What == 1
ylim([-1*Pos_Max Pos_Max])
xlabel('No. Sphere')
ylabel('Position (m)')
end
if View_What == 2
ylim([-1*Vel_Max Vel_Max])
xlabel('No. Sphere')
ylabel('Velocity (m/s)')
end
if View_What == 3
ylim([-1*Acc_Max Acc_Max])
xlabel('No. Sphere')
ylabel('Acceleration (m/s^2)')
end
% Plot a line indicating zero
hold on
plot(x0,y0,'k-')
% Plot the interpolated data
plot(xx,yy,'r--')
%Display data as movei
drawnow
F(j) = getframe;
%Slow down if SlowMotion selected
if SlowMotion == 1
pause(PauseTime)
end
end
% END : SHOW ALL AT REGULAR SPEEED or SLOW MOTION
% START : SHOW ALL AT HIGHER SPEEED
if FastForward == 1
count=count+1;
if count==SpeedFac
% The actual data points to plot are defined
x=linspace(1,No_Of_Spheres,No_Of_Spheres);
y = Phase_Data(i,Read_Start:Read_End);
% Now we interpolate spline between these data points to enable
% displaying a smooth curve
xx = 1:0.1:No_Of_Spheres;
yy = spline(x,y,xx);
% Begin plotting
hold off
% Plot original data points
plot(x,y,'bo',...
'LineWidth',.75,...
'MarkerEdgeColor','b',...
'MarkerFaceColor','b',...
'MarkerSize',6)
xlim([0 No_Of_Spheres+1]);
title(['i = ' num2str(i)])
% Determine range limits for y-axis and set labels
if View_What == 1
ylim([-1*Pos_Max Pos_Max])
xlabel('No. Sphere')
ylabel('Position (m)')
end
if View_What == 2
ylim([-1*Vel_Max Vel_Max])
xlabel('No. Sphere')
ylabel('Velocity (m/s)')
end
if View_What == 3
ylim([-1*Acc_Max Acc_Max])
xlabel('No. Sphere')
ylabel('Acceleration (m/s^2)')
end
% Plot a line indicating zero
hold on
plot(x0,y0,'k-')
% Plot the interpolated data
plot(xx,yy,'r:')
%Display data as movei
drawnow
F(j) = getframe;
count = 0;
% movie(F)
end
end
% END : SHOW ALL AT HIGHER SPEEED
end
As you can see, the FOR LOOP for 'i' from '1' to 'Time_Steps' is what plots the data and inside there are two loops for FastForward = 0 (regular speed or slow motion) and for FastForward = 1. It doesn't really matter in which one I put the textbox.
As you can see in this code
title(['i = ' num2str(i)])
I am able to have a title that successfully updates the data count on the figure but It does not work with the annotation function

Iniciar sesión para comentar.

Respuesta aceptada

AbdullahAJ6
AbdullahAJ6 el 14 de Jul. de 2018
Update: I have solved the problem. Thank you for your input @Robert U and I apologise for not including my code @Rik Wisselink it was naive of me.
If anyone is wondering how I did it:
The problem was that the annotation textbox was getting created with each loop iteration and thus all these textboxes were overlapping each other.
I circumvented this by creating the textbox outside the main loop
for i=1:1:Time_Steps
and then using the following code to assign the textbox value to the current iteration:
set(h,'String',['i = ' num2str(i)])

Más respuestas (0)

Categorías

Más información sobre Spline Postprocessing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by