简体   繁体   中英

Use external function in matlab Gui

I have a function that reads 2 files and assign them to 2 variables:

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).

the problem is how to use the function inside a 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)

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. 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. 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??

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. You don't need any files to open, just give them empty string values or nan values as appropriate.
  • You need to use the guidata function to store data in handles between callback. More information here . That way you can use handles.whatever to access variables in other callbacks.
  • You say that skel and mot are structures. set(handles.skel_mot,'String',skel) needs skel to be a string.
  • Make sure any functions you call from the gui are in the path where the gui can find them.

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. At the end , i use the handles as Molly suggested and also the command assignin for having the skel,mot at the workspace.

so: function GUI_1_OpeningFcn(hObject, eventdata, handles, varargin) %default comments

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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