简体   繁体   English

在Matlab Gui中使用外部功能

[英]Use external function in matlab Gui

I have a function that reads 2 files and assign them to 2 variables: 我有一个读取2个文件并将它们分配给2个变量的函数:

skelfile='data/name.asf'; %asf and amc files are associated,both needed for later stages
motfile='data/name.amc';
[skel,mot]=readMocap(skelfile,motfile);%the function that i need to use is the readMocap

the above code will give variables skel,mot as 1X1 structs with information both numeric and characters(contains numbers,cells,strings,aarays as struct fields). 上面的代码将给变量skel,mot作为1X1结构,同时包含数字和字符信息(包含数字,单元格,字符串,aarays作为结构字段)。

the problem is how to use the function inside a Gui!! 问题是如何在Gui中使用该功能! i use a pusshbutton that load the 2 files and show at 2 static texts the filenames of both asf,amc files asf,amc files are files that contain Motion Capture data for a human skeleton where asf has informations about the skeleton and amc about a movement(frame sequence) 我使用一个pusshbutton来加载2个文件并以2个静态文本显示asf,amc文件的文件名asf,amc文件是包含人体骨骼运动捕捉数据的文件,其中asf包含有关骨骼的信息以及有关运动的amc (帧顺序)

function pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to load_MoCap (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[filename, pathname] = uigetfile('*.asf', 'MoCap files');

% show name at the static texts
if isequal(filename,0) 
   set(handles.asf_filename, 'String', 'Please select an .asf file to continue')
else
  set(handles.asf_filename, 'String', filename)
  skelfile=filename;
[filename2, pathname2] = uigetfile('*.amc;*.c3d', 'MoCap files');
if isequal(filename2,0)
   set(handles.amc_filename, 'String', 'Please select an .amc file to continue')
else
  set(handles.amc_filename, 'String', filename2)

 %the problem
 ============
 %from here i want to run the function and have at a static text the text that
 %have   when i write skel    in the command promt of matlab, or at least somehow
 %evaluate tha skel and mot have been assigned as structs

  motfile=filename;
  [skel,mot]= readMocap(skelfile, motfile);
  handles.skel=skel;
  handles.mot=mot;
  set(handles.skel_mot,'String',skel)
   % skel_mot is the static text that refer above
   %and i use as property type at the set command th 'string' but i don't  think
   %that   is correct .   skel variable is a 1x1 struct                
  end
  end
guidata(hObject,handles);

I don't have anything else in my code than the default when you start a blank gui. 当您启动空白gui时,代码中除了默认值之外没有其他任何内容。 a)Do i have to add something (handles)at the opening function of the gui??i don't want something to start before load the files. a)我是否必须在gui的打开功能中添加一些内容(句柄)?我不希望在加载文件之前启动某些内容。 b)i want to use the information from the files as inputs for other function that will be called from the gui so how can i use them as inputs when i called the function inside the gui??as skel,mot or handles.skel,handles.mot?? b)我想将文件中的信息用作将要从gui调用的其他函数的输入,因此当我在gui中调用该函数时如何将它们用作输入?如skel,mot或handles.skel, handles.mot ??

Thank you in advance for any response. 预先感谢您的任何答复。

A few things: 一些东西:

  • Yes, you need to define the fields in handles in the opening function of your GUI. 是的,您需要在GUI的打开功能中定义handles中的字段。 You don't need any files to open, just give them empty string values or nan values as appropriate. 您不需要打开任何文件,只需为它们提供空字符串值或nan值即可。
  • You need to use the guidata function to store data in handles between callback. 您需要使用guidata函数将数据存储在回调之间的句柄中。 More information here . 更多信息在这里 That way you can use handles.whatever to access variables in other callbacks. 这样,您可以使用handles.whatever来访问其他回调中的变量。
  • You say that skel and mot are structures. 您说skelmot是结构。 set(handles.skel_mot,'String',skel) needs skel to be a string. set(handles.skel_mot,'String',skel)需要使用skel作为字符串。
  • Make sure any functions you call from the gui are in the path where the gui can find them. 确保从gui调用的所有函数都在gui可以找到它们的路径中。

I found a solution to my problem! 我找到了解决问题的方法!

i wrote a script that does what i want.Open from a pushbutton a window to choose my files, then i call the function inside the script so i have the structs skel,mot assigned with the wanted informations. 我写了一个脚本,执行我想要的操作。从按钮打开一个窗口以选择我的文件,然后在脚本中调用该函数,以便为结构skel,mot分配所需的信息。 At the end , i use the handles as Molly suggested and also the command assignin for having the skel,mot at the workspace. 最后,我使用了Molly建议的句柄,还使用了用于在工作区中放置skel,mot的命令assignin。

so: function GUI_1_OpeningFcn(hObject, eventdata, handles, varargin) %default comments 如此:函数GUI_1_OpeningFcn(hObject,eventdata,句柄,varargin)%默认注释

handles.skel=NaN;
handles.mot=NaN;
% Choose default command line output for GUI_1
handles.output = hObject;

% Update handles structure
guidata(hObject, handles); 

============
more of the default Gui code
============

 %pushbutton function 

function load_MoCap_Callback(hObject, eventdata, handles)
% hObject    handle to load_MoCap (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


%callint the script
nameofthescript;

%here skel and mot have been assigned so i put them to the handles
handles.skel=skel;
handles.mot=mot;

%with that i have them at the workspace for evaluation of what i want to do  next
assignin ('base', 'skel', handles.skel);
assignin('base','mot',handles.mot);


guidata(hObject,handles);

from then i can use handles.skel and handles.mot for inputs in any other function that i want 从那时起,我可以使用handles.skel和handles.mot在我想要的任何其他函数中进行输入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM