Error in loading variables from GUI to workspace in Matlab - matlab

I have created a GUI in Matlab and made a simple pushbutton function in GUI for running my matlab file.
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)
Uncertainty_MMS_refactored_Final;
I can load my text files (e.g. PTX_Data_Raw.txt) via GUI in workspace by using assignin function.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
[filename_2,PathName]=uigetfile({'*.txt'},'Import Reference PTX file without header');
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
PTX_Raw = importdata(filename_2);
assignin('base', 'PTX_Data_Raw', PTX_Raw);
content = filename_2;
set(handles.edit2, 'string', content,'fontsize',12);
But when I run my code via GUI byy pusshing "push button" button I receive this error:
Undefined function or variable 'PTX_Data_Raw'
while I check my workspace, I can see all files that I have loaded from GUI to workspace and even I can run my program with F5 button via keyboard! but I don't know why I can run my code via GUI?! HERE is the code that I have written for running my matlab file. here is the full error that I receive:
Undefined function or variable 'PTX_file'.
Error in Uncertainty_MMS_refactored_Final (line 21)
RefOrgnizedData = OrgnizingData(PTX_file, PTX_Data_Raw);
Undefined function or variable 'PTX_file'.
Error in GUI>pushbutton7_Callback (line 178)
Uncertainty_MMS_refactored_Final;
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in GUI (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in #(hObject,eventdata)GUI('pushbutton7_Callback',hObject,eventdata,guidata(hObject))
Error using waitfor
Error while evaluating UIControl Callback
Should I change or add something else more? please help me

Related

matlab run script file from GUI push button

i have a script file , want to run it from gui push button
its not working .
the error is :
Undefined variable "classifyDeeb" or class "classifyDeeb.m".
Error in Train>pushbutton2_Callback (line 113)
classifyDeeb.m
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in Train (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in #(hObject,eventdata)Train('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
------------------
scrip file code `load deeb;
trdata=[deeb(1:8,2:6);deeb(11:18,2:6)];
gr=[deeb(1:8,1);deeb(11:18,1)];
testdata=[deeb(9:10,2:6);deeb(19:20,2:6)];
svmstr=svmtrain(trdata,gr);
result = svmclassify(svmstr,testdata);
output = result;`
----------------------------
the pushbutton2_Callback code is :
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
classifyDeeb.m
the script file, the figure and the matrix data ( deeb.mat) file are in the same folder.
please help
thanks in advance for help
Two options:
You can use run() on the m-file name. You can even include the full path to the script if it is in another directory.
function pushbutton2_Callback(hObject, eventdata, handles)
run('classifyDeeb.m')
or call it without the extension. This will work as long as it is located in on of the Matlab's Paths.
function pushbutton2_Callback(hObject, eventdata, handles)
classifyDeeb

How to use user-created class in Guide-created GUI

I am creating an application in GUIDE. I've found that using the "handles" structure provided by GUIDE to store data quickly leads to messy/hard to read code. I decided that the best solution is to create my own class to handle data as well as store methods to be used in callback functions. I've been able to successfully call the constructor method in "annotatorGUI_OpeningFcn" (seen below), but when I call a class method in a different callback function, it can't find any reference to my class. Furthermore, the line "annotatorEngine = ...." is underlined in yellow with the statement "value assigned to variable might be unused". It seems that my class declaration doesn't propagate throughout the entire GUI script. I want to avoid using the "handles" structure or declaring "annotatorEngine" as global. Thanks!
EDIT: So far it seems the only thing that has worked is declaring my class object as global. However, this is still slightly annoying because in each callback, I have to write "global annotatorEngine".
% --- Executes just before annotatorGUI is made visible.
function annotatorGUI_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 annotatorGUI (see VARARGIN)
% Choose default command line output for annotatorGUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% CLASS CONSTRUCTED HERE
annotatorEngine = annotatorGUIClass(handles.rawAxes, handles.psdAxes, handles.allPairsAxes)
% UIWAIT makes annotatorGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
Place where I call a method.
% --------------------------------------------------------------------
function loadData_Callback(hObject, eventdata, handles)
% hObject handle to loadData (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('*.mat', 'Select a data file to load');
annotatorEngine.loadData(FileName, PathName)
return

MATLAB GUI Error while using set

why I cant have the output in the edit text?
% --- Executes on button press in f.
function f_Callback(hObject, eventdata, handles)
% hObject handle to f (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc
syms t
a=str2double(get(handles.b1,'string'));
b=str2double(get(handles.c1,'string'));
y=eval(get(handles.a1,'string'));
u=a-b;
m=abs(y).^2;
r=int(m,t);
g1=subs(r,t,a);
h=subs(r,t,b);
fh=g1-h;
s=fh./u
set(handles.e,'string',s)';
Command window shows the answer not edit text.The error is:
Error using set
error: mxArray must be double, char, or cell
Error in signalproject2>f_Callback (line 284)
set(handles.e,'string',s)';
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in signalproject2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in #(hObject,eventdata)signalproject2('f_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Can u please help me with this?
What is the data type of s? Use whos s to find out.
If it's not a double, char, or cell then you won't be able to assign it as a string for your edit box. This is exactly what the error message tells you.
I don't have the symbolic math toolbox so I can't check the code, but I'm guessing s is a symbolic array. If this is the case, see the documentation for char to convert the symbolic array to a string so you can assign it properly.
Could it simply be because there is a ' you don't want at the end of this line:
set(handles.e,'string',s)';
what if you try:
set(handles.e,'string',s);

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');

MATLAB GUI calculator error

I'm Developing a calculator that converts back and forth from a Julian time to a standard IRIG time using MATLAB's GUIDE. When started, the calculator works fine going one way, or starting out going the other way, but somehow something gets deleted when going back and forth in the same session. I'm only using two buttons, and this is what the code looks like for the callbacks of those two buttons:
% --- Executes on button press in convertjulian.
function convertjulian_Callback(hObject, eventdata, handles)
% hObject handle to convertjulian (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
value = handles.isec;
day = floor(value/86400);
remainder = (value/86400 - day)*86400;
hour = floor(remainder/3600);
remainder = (remainder/3600 - hour)*3600;
min = floor(remainder/60);
sec = (remainder/60 - min)*60;
set(handles.jday,'String',day);
set(handles.jhour,'String',hour);
set(handles.jmin,'String',min);
set(handles.jsec,'String',sec);
Here is the other callback:
% --- Executes on button press in convertirig.
function convertirig_Callback(hObject, eventdata, handles)
% hObject handle to convertirig (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
seconds=handles.jday*86400+handles.jhour*3600+handles.jmin*60+handles.jsec;
set(handles.isec,'String',sprintf('%0.3f',seconds));
And here is the error that I get in MATLAB when I'm running it:
Error using handle.handle/set
Invalid or deleted object.
Error in timeconversion>convertjulian_Callback (line 124)
set(handles.jday,'String',day);
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in timeconversion (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in #(hObject,eventdata)timeconversion('convertjulian_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
You are treating handles as numeric values. To get the value from a handles.isec, you can use:
value = str2double(get(handles.isec, 'String'));
All the 'j' handles will be similar.