MATLAB GUI如何实现字幕滚动

本文介绍在MATLAB GUI中实现字幕滚动的方法,通过创建计时器对象,设置固定周期执行回调函数,更新GUI上的字符串显示,实现字幕的滚动效果。

MATLAB GUI如何实现字幕滚动

主要函数及解析

handles.timer = timer(‘Period’,1,‘ExecutionMode’,‘FixedRate’, ‘TimerFcn’,{@timercallback,handles});
function timercallback(obj, event, handles);

start(handles.timer);
stop(handles.timer);
重点在timer设置,以及其对应的回调函数;
本部分介绍如何创建计时器对象,启动计时器运行,以及指定您希望在计时器触发时执行的过程。当计时器对象指定的时间已过且计时器对象执行您指定的命令时,即触发计时器;
要使用计时器,请执行以下步骤:
1.创建一个计时器对象。
可使用 timer 函数创建计时器对象。
2使用Set指定希望在计时器触发时执行的 MATLAB 命令并控制计时器对象的其他各方面的行为
3.启动计时器对象。
创建计时器对象后,必须使用 start 或 startat 函数启动该对象。
4.在处理完计时器对象后将其删除。
具体参阅 https://ww2.mathworks.cn/help/matlab/matlab_prog/use-a-matlab-timer-object.html

代码

下面展示一些 内联代码片

``function varargout = slide_word(varargin)
gui_Singleton = 1;
gui_State = struct(‘gui_Name’, mfilename, …
‘gui_Singleton’, gui_Singleton, …
‘gui_OpeningFcn’, @slide_word_OpeningFcn, …
‘gui_OutputFcn’, @slide_word_OutputFcn, …
‘gui_LayoutFcn’, [] , …
‘gui_Callback’, []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% — Executes just before slide_word is made visible.
function slide_word_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
global word;
word = {‘Hello!’,‘你好’};
handles.timer = timer(‘Period’,1,‘ExecutionMode’,‘FixedRate’, ‘TimerFcn’,{@timercallback,handles});
start(handles.timer);
% Update handles structure
guidata(hObject, handles);
uiwait(handles.figure1);

function timercallback(obj, event, handles)
global word;
Str1 = word{1};
Str2 = word{2};
word = strvcat(Str2, Str1);
set(handles.text, ‘String’, word);
tempStr{1} = word(1, : );
tempStr{2} = word(2, : );
word = tempStr;

% — Outputs from this function are returned to the command line.
function varargout = slide_word_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
stop(handles.timer);
% Get default command line output from handles structure
varargout{1} = handles.output;

运行结果

动态字幕显示

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值