Use external function in matlab Gui - matlab

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

Related

Matlab GUI programming: use data imported with uiimport

I am programming a simple GUI which has to do 2 tasks:
1- import a set of data for a txt file
2- make some computation with the previously imported data
Both tasks are performed pressing a button, one button for each task.
For the "Load data" button (task 1) I used the uiimport command in the Callback of that button (as explained here Matlab Calling 'Import Data' with GUI Button):
S = uiimport('-file');
The data are loaded as a "191384x3 double".
I also modified the function as follow, in order to have S available for the 2nd button:
function S = load_data_Callback(hObject, eventdata, handles)
Then I press the second button to perform the 2nd task. In the Callback of the 2nd button I wrote
function pushbutton2_Callback(hObject, eventdata, handles, S)
loaded_data = S; % to access the data in the non structured array
% Then I want to have 3 separate vectors out of the structure
v1 = loaded_data(:,1);
v2 = loaded_data(:,2);
v3 = loaded_data(:,3);
When I press the 2nd button I get an error message:
Not enough input arguments.
loaded_data = S;
Error in gui_mainfcn (line 95)
feval(varargin{:});
What am I missing?
You need to store the output from the first function somewhere, where the second function will be able to access it. This is typically done by storing it either in some ui object UserData property, or to store in some special data storage structure that is attached attached to figure objects, and is accessed with either the guidata function, or the getappdata and setappdata functions.
dat=guidata(hObj); gets the guidata from hObj parent figure
guidata(hObj,dat); sets the guidata of the parent figure of hObj to dat
Working with guide, the guidata structure is populated by default with a structure containing all the ui objects, named after their tag. Extra fields may be added as needed. The handle argument from callbacks contains guidata(hObj).
Three possible implementations:
1. With guidata
Store the loaded data with guidata
function load_data_Callback(hObject, eventdata, handles)
% Load the file and save the result in variable S%
% Then: %
handles.S=S;
guidata(hObject,S);
It will be automatically loaded back in the handles input variable
function pushbutton2_Callback(hObject, eventdata, handles)
assert(isfield(handles,S),'Load some data first!');
loaded_data=handles.S;
%...%
2. With Userdata property (up to R2014a)
Store the loaded data into some uiobject UserData, e.g. pushbutton2
function load_data_Callback(hObject, eventdata, handles)
% Load the file and save the result in variable S%
% Then: %
set(handles.pushbutton2,'UserData',S,'Enable','on'); %You could disable pushbutton2 by default, until some data has been loaded
Recover the data from the UserData property
function pushbutton2_Callback(hObject, eventdata, handles)
loaded_data=get(hObject,'UserData');
assert(~isempty(loaded_data),'Load some data first!');
%...%
3. With appdata functions
Store the loaded data with setappdata
function load_data_Callback(hObject, eventdata, handles)
% Load the file and save the result in variable S%
% Then: %
setappdata(hObject,'loaded_data',S);
Recover with getappdata:
function pushbutton2_Callback(hObject, eventdata, handles)
assert(isappdata(hObject,'loaded_data'),'Load some data first!');
loaded_data=getappdata(hObject,'loaded_data');
%...%

How to save some values from gui into a text file and load them elsewhere as a number?

I have 2 values from an edit box and I want to write them in a txt file when I press a push button
function Masaedit_Callback(hObject, eventdata, handles)
% hObject handle to Masaedit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
function Arias_Callback(hObject, eventdata, handles)
% hObject handle to Arias (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA
m=str2double(get(handles.Masaedit, 'string'));
assignin('base','m',m)
A=str2double(get(handles.Arias, 'string'));
assignin('base','A',A)
twovalues = fopen('twovalues.txt','w');
fprintf(twovalues,'%6d\t%3d',m,A);
fclose(twovalues);
But I want to save them as:
m=value;
A=value;
After the values are saved , I want to load the text file in a function like:
function xypr=twovalues (m,A)
CD=1;
load ('twovalues.txt',m,A)
ad=(-1/2)*((CD*A)/m);
end
But that it seems a little tricky and I don't know how should I do the conversion because for A,m = I need to load them as a string and for the result I should use something like str2double.
I actually made a quick youtube video in response to your question, so if you want to watch that you can(I go into more depth there, but it is meant to be a bit more general):
https://youtu.be/GQtYAT36CZ4
Otherwise, a short answer is that it depends on whether you need that data available outside of matlab or not. Your approach is mixing those two - generally you should either set it up to not be accessible outside of matlab using the save command (save('filename.mat', 'm', 'A') and load('filename.mat', 'm', 'A')) or write to a csv with column headers (use writetable and readtable with a table containing only your variables for example). Note that for the purposes of your answer, csv is a text format. Of course it is possible to use fprintf as you describe, but that is really the hardest way of doing it. I hope that helps!
Edit: I note in your question that part of your issue is related to the string vs double nature of the text box. The method I suggest here would cut in after you convert to double. No method will get rid of that issue, unless you use a gui element which outputs as double.

MATLAB - passing new value of parameter from GUI into .m script

I want to make simple GUI for my script, where I can edit parameter values and running that script.
I've created example scipt and GUI with 2 buttons. I'cant put script code into GUI code, I will need to aply it on much larger script.
So, script code:
number = 10;
variable(1:10) = NaN;
for i = 1:10;
variable(i) = i * number;
end
figure
plot(variable)
Push button code, that is working fine. script is name of .m file, not function:
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
evalin('base','script')
But I dont know what to type into edit button code If i want to change value of "number" in the script:
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
And last thing, sometimes when I try to plot more graphs, one figure overwrites GUI figure and I can see only buttons, but not whole GUI.
Thank you fot any help.
The hint given in the last piece of code is enough.
a = str2double(get(hObject,'String'));
This will save the input value as a double, in the callback function's stack.
In order to pass this value to the caller script's (base) stack, use assignin
assignin('base', 'number', a)

How to call a variable used in one function in another function?

I have created a matlab gui and I want to use the variable magE from the function (pushbutton1) in the function (pushbutton2).
How can I call it?
magE = matrix of 244 rows and 2000 Columns
I would be grateful for any help. Thank you!
You should be using the hObject handle to pass data between GUI functions and callbacks, it's all quite well explained in the auto-generated comments. Example taken from MATLAB documentation:
% --- Executes just before simple_gui_tab is made visible.
function my_GUIDE_GUI_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 simple_gui_tab (see VARARGIN)
% ...
% add some additional data as a new field called numberOfErrors
handles.numberOfErrors = 0;
% Save the change you made to the structure guidata(hObject,handles)
Suppose you needed to access the numberOfErrors field in a push button callback. Your callback code now looks something like this:
% --- Executes on button press in pushbutton1.
function my_GUIDE_GUI_pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% ...
% No need to call guidata to obtain a structure;
% it is provided by GUIDE via the handles argument
handles.numberOfErrors = handles.numberOfErrors + 1;
% save the changes to the structure
guidata(hObject,handles)
One way would be to declare magE as a global variable in the main script.
Then, inside each function you should also declare it as global so that it would refer to the same global variable.
e.g.
global magE
<your_code_here>
function [] = pushbutton1()
global magE
%%<your_code_here>
end
function [] = pushbutton2()
global magE
%%<your_code_here>
end

matlab function handle inside gui

I created a GUI layout using Matlabs GUID application, essentially my problem is simple, i have a text box where I want to enter data, and a push button which i want to display that value entered inside the text box, the text box is define as
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
myString = get(hObject, 'String')
set(hObject,'Value',str2num(myString));
at this point, i entered some numerical value into the text box, lets say 44, now 44 is stored inside the "Value" element of hObject for this function.
Now I want to output this value when a button is pushed
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
fHandle = #edit1_Callback
get(fHandle,'Value')
^^^on this last line i should see some output(since i dont have a ";" which allows matlab to print out the data), the issue is....i get an error that says "Conversion to double from function_handle is not possible." , shouldnt I be able to call "get" using a function handle which points to my earlier function, thanks for any help!
I would go for
get(handles.edit1,'value')
All your uicontrols are stored in the handles. Knowing the tag of your uicontrol (for your edit box I guess this is edit1), you can get the handles of this object with handles.edit1.
You can even write
my_value = get(handles.edit1,'Value');