How to use script in MATLAB allowing functions to be declared? - matlab

While making GUI I've came into this problem:
Using script (S) in function (A) makes other functions (B,C,D..., which are declared in function (A)) as undeclared while executing script (S), it happens on MATLAB version R2016b, however everything works fine in MATLAB R2015b.
Basically I have a bunch of axes, and axesPushScript.m script, which executes on click.
Inside script i got this line:
autorange_Callback(handles.autorange, eventdata, handles);
Inside my main function I've got this:
function autorange_Callback(hObject, eventdata, handles)
if (hObject.Value==1)
axis(handles.axesSpectra, 'tight');
axesSpectra_ButtonDownFcn(handles.axesSpectra,eventdata,handles);
else axis(handles.axesSpectra, 'manual');
end
get this error message on MATLAB R2016b:
Undefined function or variable 'autorange_Callback'.
Error in axesPushScript (line 44)
autorange_Callback(handles.autorange, eventdata, handles);
Error in SNOM_alpha_4>axesTrace_ButtonDownFcn (line 463)
axesPushScript;
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in SNOM_alpha_4 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>#(hObject,eventdata)SNOM_alpha_4('axesTrace_ButtonDownFcn',hObject,eventdata,guidata(hObject))
Error while evaluating Axes ButtonDownFcn
Any ideas of how I could fix it?
%---------------------------------------------------------------
EDIT:
adding .rar file with a simple one button example:
https://www.dropbox.com/s/gsgxb9xt1s6hbvp/junk.rar?dl=0

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

Error in loading variables from GUI to workspace in 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

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

simulink run with gui matlab

i have a Gui and a Simulink Model, i wanna enter some value into the textfield in the Gui and press start Button and after this, my simulink model should take these values and run, the result should be displayed in the gui statictext.
to simulink: i have to constant blocks, thes name ist kraft and flaeche. and the oarameter into the blocks are k and f.
ok now i want to edit the values of k and f in the gui. This is my code:
function kraft_Callback(hObject, eventdata, handles)
kraft_value = str2num(get(hObject,'String'));
if (isempty(kraft_value))
set(hObject,'String','0')
end
guidata(hObject, handles);
function flaeche_Callback(hObject, eventdata, handles)
flaeche_value = str2num(get(hObject,'String'));
if (isempty(flaeche_value))
set(hObject,'String','0')
end
guidata(hObject, handles);
function start_Callback(hObject, eventdata, handles)
k= str2double(get(hObject,'string'));
f= str2double(get(hObject,'string'));
sim('Steifigkeit');
function static_CreateFcn(hObject, eventdata, handles)
But i get these Errors :
Error using Gui>start_Callback (line 215)
Error due to multiple causes.
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in Gui (line 17)
gui_mainfcn(gui_State, varargin{:});
Error in #(hObject,eventdata)Gui('start_Callback',hObject,eventdata,guidata(hObject))
Caused by:
Error using Gui>start_Callback (line 215)
Error evaluating parameter 'Value' in 'Steifigkeit/f'
Error using Gui>start_Callback (line 215)
Undefined function or variable 'f'.
Error using Gui>start_Callback (line 215)
Error evaluating parameter 'Value' in 'Steifigkeit/k'
Error using Gui>start_Callback (line 215)
Undefined function or variable 'k'.
can somebody help me
I think Simulink is looking for k and f in the base workspace, but they are only defined in your callback function workspace. You probably need to use assignin:
function start_Callback(hObject, eventdata, handles)
k= str2double(get(hObject,'string'));
f= str2double(get(hObject,'string'));
assignin('base','f',f);
assignin('base','k',k);
sim('Steifigkeit');
I don't know how your GUI is constructed, but to me it looks like k and f are the same based on your code. Is this how you meant it to be?
The general form of the command syntax for running a simulation is:
SimOut = sim('model', Parameters)
So this way you can run simulink models from anywhere using commands. you can also add your parameters to the model and run it.
The following example shows how to create a configuration set and use it with the sim syntax.
model = 'vdp';
load_system(model)
simMode = get_param(model, 'SimulationMode');
set_param(model, 'SimulationMode', 'rapid')
cs = getActiveConfigSet(model);
model_cs = cs.copy;
set_param(model_cs,'AbsTol','1e-5',...
'SaveState','on','StateSaveName','xoutNew',...
'SaveOutput','on','OutputSaveName','youtNew')
simOut = sim(model, model_cs);
set_param(model, 'SimulationMode', simMode)
so you just need to replace the values from textbox and that would be this...
I usually use this to set a value in simulink:
Control_Gains(1,1)=str2double(get_param([ModelName,'/PID1/PIDx'],'P'));
or:
NewString = ['[',(num2str(KT)),']'];
set_param([ModelName,'/System/Model/Gain'],'Gain',NewString);
I went through your code but there seems to be no function that will update the constant blocks.. I have created a similar GUI to tune inputs at runtime.
Try this approach:
In the callbacks of your editboxes in the GUI, write these commands
valstr=get(hObject,'String');
val=str2double(valstr);
assignin('base','nameofvariableinmatlabworkspace',val);
set_param('constantblockpath','Value','nameofvariableinmatlabworkspace');
Replace nameofvariableinmatlabworkspace with the name of the variable that appears inside the constant blocks whose value should change according to the value entered in the GUI.
Replace constantblockpath with the path to that constant block e.g. mymodel/Constant12
You might want to do some data validation for the editboxes as well, before assigning their values in the matlab workspace variables.

Error in matlab, while using set in GUI

I have tried the following code in Matlab:
function pushbutton5_Callback(hObject, eventdata, handles)
global smoothening_level;
global Amp_threshold;
global Min_PeakDistance;
global Mat_wave
global Mat_wave2
global Mat_inten
global pks
Mat_inten2 = smooth(Mat_inten,smoothening_level);
[pks,locs] = findpeaks(Mat_inten2,'minpeakdistance',Min_PeakDistance,'minpeakheight',Amp_threshold)
s = size(pks)
figure(1)
Mat_wave2 = Mat_wave(locs(:));
Mat_inten2loc = Mat_inten(locs(:));
hold all;
plot(Mat_wave,Mat_inten2);
plot(Mat_wave2,pks,'o','MarkerEdgeColor','r');
legend('Ouptut Spectrum','Smoothened Spectrum','Identified Peaks')
axis([350 900 0 max(Mat_inten)]);
xlabel('Wavelength')
ylabel('Intensity')
grid on
title('Plasma Emission Spectrum')
temp(:,1)=Mat_wave2;
temp(:,2)=Mat_inten2loc;
set(handles.uitable8,'Data',num2cell(temp))
However it is giving the following error:
??? Attempt to reference field of non-structure array.
Error in ==> GUI>pushbutton5_Callback at 242
set(handles.uitable8,'Data',num2cell(temp))
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> GUI at 50
gui_mainfcn(gui_State, varargin{:});
Error in ==> #(hObject,eventdata)GUI('pushbutton5_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
The error tells you that handles is not of type struct. Hence, you can not access handles.uitables8. Most likely what you want to write is
set(handles,'Data',num2cell(temp))', but that is just a guess without knowing the rest of your code.
Check to make sure that handles.uitable8 exists. The easiest way to do this is to set a breakpoint in your code at line 242. When the code stops in debugger, go to your workspace and open the handles structure.
If you created this gui using GUIDE, then most likely the tag is mislabeled or something similar.