MATLAB 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;

运行结果

MATLAB GUI如何实现字幕滚动