在我开始讨论任何事情之前,我想为这个问题的基本和基本看法以及我自己的无知道歉。
我决定学习Matlab,因为它看起来很有趣。现在,我没有任何编程知识,也不了解任何其他编程语言。因为我还在Highschool,所以我很可能很年轻。虽然其他编程语言有一步一步的在线教程(所以我听说过),但Matlab除了一些视频和文档(我正在努力理解和导航)之外什么都没有。我想学习Matlab而不是任何其他语言,因为这显然是我在爸爸的工作场所需要帮助的。
无论如何,要解决这个问题。我正在构建一个基本的视频播放器,它不播放像.avi这样的视频,而是包含许多不同帧,.tif文件或类似的数据包。
这就是我对播放按钮的使用(我使用的是按钮)
%// --- Executes on button press in play.
function play_Callback(hObject, eventdata, handles)
%// hObject handle to play (see GCBO)
%// eventdata reserved - to be defined in a future version of MATLAB
%// handles structure with handles and user data (see GUIDATA)
play = 1;
im_info = imfinfo('./Rat_FDB_MG29_RyR_04_R_image.tif'); %//want to make this a variable that is able to be changed through the input file, maybe through iminfo(inputfile)?
imLength = length(im_info);
if play == 1
for i=1:imLength
im = imread('./Rat_FDB_MG29_RyR_04_R_image.tif', 'index', i); %//same as above with the file variable change
imagesc(im);
axis equal;
axis ij;
axis off;
colormap gray;
pause(.1); %// I want to make it pause(inputinc) so the user can change the frame rate from the gui, but that doesn't work
drawnow;
end
else
end
此代码主要是通过复制和粘贴以及一些猜测和检查生成的。奇迹般地它运作和视频播放。我理解的功能是暂停功能,imread
功能和iminfo(kinda)
。问题是我在GUI中有两个edit-text
,一个输入确定视频的帧速率,另一个输入确定视频的名称。
以下是edit-text
function increment_Callback(hObject, eventdata, handles)
%// hObject handle to increment (see GCBO)
%// eventdata reserved - to be defined in a future version of MATLAB
%// handles structure with handles and user data (see GUIDATA)
%// Hints: get(hObject,'String') returns contents of increment as text
%// str2double(get(hObject,'String')) returns contents of increment as a double
inputinc = str2num(get(hObject,'String'));
%//this is the increment in which the frames play, I have to figure out how
%//to get it into the play function
在play
函数中有pause
函数,我理解这是在播放下一帧之前经过的时间。因此pause(.5)
允许在播放下一帧之前传递.5
秒。但是,当我执行pause(inputinc)
时,它会出现错误,并且帧后的第2帧不会播放。起初我认为这是因为edit-text
中的名称是一个字符串,但即使使用str2num
后它仍然会出错。是因为函数页面中的edit-text
函数低于播放按钮函数吗?
这是完整的功能脚本,如果它可以帮助人们理解我正在尝试做什么。
function varargout = untitled(varargin)
%// UNTITLED MATLAB code for untitled.fig
%// UNTITLED, by itself, creates a new UNTITLED or raises the existing
%// singleton*.
%//
%// H = UNTITLED returns the handle to a new UNTITLED or the handle to
%// the existing singleton*.
%//
%// UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
%// function named CALLBACK in UNTITLED.M with the given input arguments.
%//
%// UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
%// existing singleton*. Starting from the left, property value pairs are
%// applied to the GUI before untitled_OpeningFcn gets called. An
%// unrecognized property name or invalid value makes property application
%// stop. All inputs are passed to untitled_OpeningFcn via varargin.
%//
%// *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
%// instance to run (singleton)".
%//
%// See also: GUIDE, GUIDATA, GUIHANDLES
%// Edit the above text to modify the response to help untitled
%// Last Modified by GUIDE v2.5 03-Aug-2015 22:31:53
%// Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_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 untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
%// This function has no output args, see OutputFcn.
%// hObject handle to figure
%// eventdata reserved - to be defined in a future version of MATLAB
%// handles structure with handles and user data (see GUIDATA)
%// varargin command line arguments to untitled (see VARARGIN)
%// Choose default command line output for untitled
handles.output = hObject;
inputinc = .1
%// Update handles structure
guidata(hObject, handles);
%// UIWAIT makes untitled wait for user response (see UIRESUME)
%// uiwait(handles.figure1);
%// --- Outputs from this function are returned to the command line.
function varargout = untitled_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)
%// Get default command line output from handles structure
varargout{1} = handles.output;
%//my functions start here, I'm a beginner programmer that got his hands on
%//matlab through his dad's workplace access (dad doesn't use matlab, just has
%//access to it)
%//I don't know how to use anything besides matlab
%//i don't know how to use matlab
%//help pleaes
%//I'm trying to make a basic gui that plays frames except I have no idea how
%//the format works and stuff
%// --- Executes on button press in play.
function play_Callback(hObject, eventdata, handles)
%// hObject handle to play (see GCBO)
%// eventdata reserved - to be defined in a future version of MATLAB
%// handles structure with handles and user data (see GUIDATA)
play = 1;
im_info = imfinfo('./Rat_FDB_MG29_RyR_04_R_image.tif'); %want to make this a variable that is able to be changed through the input file, maybe through iminfo(inputfile)?
imLength = length(im_info);
play = inputdlg('
if play == 1
for i=1:imLength
im = imread('./Rat_FDB_MG29_RyR_04_R_image.tif', 'index', i); %same as above with the file variable change
imagesc(im);
axis equal; axis ij;axis off;
colormap gray;
pause(.1); % I want to make it pause(inputinc) so the user can change the frame rate from the gui, but that doesn't work
drawnow;
end
else
end
%// plays, but play/pause function is invalid, also, unable to manipulate
%// file name
%// --- Executes on button press in pause.
function pause_Callback(hObject, eventdata, handles)
%// hObject handle to pause (see GCBO)
%// eventdata reserved - to be defined in a future version of MATLAB
%// handles structure with handles and user data (see GUIDATA)
play = 0
%//pauses the video, not as important but I can't get it to work either
%// --- Executes on button press in exit.
function exit_Callback(hObject, eventdata, handles)
%// hObject handle to exit (see GCBO)
%// eventdata reserved - to be defined in a future version of MATLAB
%// handles structure with handles and user data (see GUIDATA)
exit = questdlg('Are you sure you want to quit?','Exit Confirmation','Yes','No','No');
switch exit
case 'Yes'
close(untitled)
case 'No'
end
%//exist function, the only thing that works so far as intended :D
function increment_Callback(hObject, eventdata, handles)
%// hObject handle to increment (see GCBO)
%// eventdata reserved - to be defined in a future version of MATLAB
%// handles structure with handles and user data (see GUIDATA)
%// Hints: get(hObject,'String') returns contents of increment as text
%// str2double(get(hObject,'String')) returns contents of increment as a double
inputinc = str2num(get(hObject,'String'));
%//this is the increment in which the frames play, I have to figure out how
%//to get it into the play function
%// I can't get pause(inputinc) to work,
%// --- Executes during object creation, after setting all properties.
function increment_CreateFcn(hObject, eventdata, handles)
%// hObject handle to increment (see GCBO)
%// eventdata reserved - to be defined in a future version of MATLAB
%// handles empty - handles not created until after all CreateFcns called
%// Hint: edit controls usually have a white background on Windows.
%// See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
%//no idea what this is
function file_Callback(hObject, eventdata, handles)
%// hObject handle to file (see GCBO)
%// eventdata reserved - to be defined in a future version of MATLAB
%// handles structure with handles and user data (see GUIDATA)
%// Hints: get(hObject,'String') returns contents of file as text
%// str2double(get(hObject,'String')) returns contents of file as a double
inputfile = get(hObject,'String');
%// user inputs file name here
%// i have to figure out how to get it inside the play function
%// --- Executes during object creation, after setting all properties.
function file_CreateFcn(hObject, eventdata, handles)
%// hObject handle to file (see GCBO)
%// eventdata reserved - to be defined in a future version of MATLAB
%// handles empty - handles not created until after all CreateFcns called
%// Hint: edit controls usually have a white background on Windows.
%// See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
%//no idea what this is
我正在为练习而不是其他任何东西建造这个。我父亲工作场所的一些人告诉我,我应该去一个项目来帮助改进。我希望有一天我可以使用而不是负担。
此外,我希望能够弄清楚如何使用另一个edit-text
从gui更改文件,但我尚未完成该部分。暂停按钮是另一个问题,我正在试图找出一个暂停视频的系统,另外,我发现的另一个问题是,如果我按播放它启动视频,有没有办法记录视频的帧而是从那里开始呢?
感谢大家的帮助,非常初学的程序员非常感谢。
答案 0 :(得分:0)
您需要阅读有关scope
变量的内容,以了解代码中出现的问题。我鼓励你从:
简单来说,您必须在代码中看到独立中的不同功能。一个函数使用它自己的变量,但不知道其他函数的变量。有不同的方法可以使这些函数相互传递变量(最常见的方法是在函数的输入参数中发送所需的变量,但上面的链接将向您展示其他一些方法)。
从您的gui代码中,我推断您有两个名为increment
和file
的文本框。我要做的第一件事是建议用一种标识符重命名它们,让你知道它们是什么类型的对象(文本框字段)。例如,我会使用txtIncrement
和txtFile
,但这部分完全取决于您。
要获取其中一个文本框的内容,请调用get
方法,然后传递要从中获取信息的对象的句柄。你已经在回调函数中完成了它:
inputfile = get(hObject,'String');
在那里,hObject
是文本框的句柄。不幸的是,变量inputfile
一旦函数完成就会消失,因此它不适用于其他函数。您可以使用inputfile
或setappdata
保存变量guidata
以使其可用,但在像您这样的简单情况下,您可以直接从将使用它的函数中读取文本框内容。您只需要文本框的句柄,但您也可以使用它:它位于名为handles
的结构中(函数中的第三个参数)。
所以直接在你的主要“游戏”功能中:
function play_Callback(hObject, eventdata, handles)
%// retrieve all the information we need from the GUI before going further
inputfile = get( handles.file ,'String') ; %// notice the handle of the "file" textbox
inputinc = str2num(get( handles.increment ,'String')) ; %// notice the handle of the "increment " textbox
%// from now on, in this function, the variables "inputfile" and "inputinc" are known so you can use:
play = 1;
im_info = iminfo(inputfile);
%//
%// and later on in the code, you can also use:
pause(inputinc) ;
请注意,如果您从主播放功能中检索文本框内容,则可以删除文本框回调中的代码位。