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.
Related
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
So i tried to share GUI data using setappdata and getappadata. for example lets consider this
matfile1.m
h = EmotivEEG;
h.Run
for k = 1:4
out(:,:,k) = h.data + rand(1);
setappdata(0,'eegData', out(:,:,k);
pause(0.5);
end
h.delete
so the above file creates a 128x14 matrix every o.5 seconds and store it in eegData
matfile2.m
some_var = getappdata(0,'eegData')
plot(some_var)
this seems to work but not while in the loop, if i ask it to plot it i get this error
Error using setappdata
Too many output arguments.
Error in eeg_live>eeg_live_OpeningFcn (line 83)
lmno = setappdata(0,'eegData');
Error in gui_mainfcn (line 221)
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in eeg_live (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in Neucube>activation_Callback (line 3963)
eeg_live
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in Neucube (line 49)
gui_mainfcn(gui_State, varargin{:});
Error in #(hObject,eventdata)Neucube('activation_Callback',hObject,eventdata,guidata(hObject))
Error using pause
Error while evaluating uicontrol Callback
any idea on how to tackle this problem.
thanks in advance.
There seems to be some problems with your code, but the line that MATLAB tells you generates the error is not in the snippet you provided, and the message is quite clear:
Using this command (line 83):
lmno = setappdata(0,'eegData');
is forbidden because setappdata does NOT accept output arguments, therefore the error is thrown. You can only use an assignment with getappdata.
Other points to consider:
1) Make sure you use the same variable name with get/setappdata (i.e. either eegdata or eegData...it might be a typo though)
2) You don't seem to call the 2nd script in your loop, so setappdata is overwriting the value of eegData at every iteration.
Hope that helps!
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);
Continuing my struggle against GUI's, I have run into another road block.
Ive successfully created a button that opens a file as a string, and places it in a text box in my GUI like so.
[filename, pathname] = ...
uigetfile({'*.m';'*.mdl';'*.mat';'*.*'},'File Selector');
set(handles.Textbox1, 'string', fullfile(pathname,filename));
But now I cannot seem to use a function on the acquired file. Ive tried doing
str = get(handles.Textbox1,'string');
Histogram(str); %Histogram is a function that I created.
But im getting the following errors
??? Error using ==> Histogram Too many input arguments.
Error in ==> VarunGUI>pushbutton2_Callback at 94 Histogram(str);
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> VarunGUI at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
#(hObject,eventdata)VarunGUI('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
Is my code for calling the function to blame, or is the function itself? I'm having trouble understanding how to alter the function to work on the called image, so that may be my problem, the function begins with the following code.
function Histogram
clear;
clc;
fid = fopen('');
myimage = fread(fid, [512, 683], '*uint8');
fclose(fid);
Is there a certain variable I need to place in the '' to make the GUI act in the manner to which I would like it? Question ran a little long, but please tell me if there is anything else you need to see in order to assist me, any guidance or tips would be great. Thanks!
Your Histogram function doesn't have an input, so it fails when you call it : Histogram(str)
You're problem is that call Histogram and pass it str:
Histogram(str)
But you don't define Histogram to expect input:
function Histogram
What you need is something like this:
function Histogram(str)
% do something with str
I got this y'all!
Change your histogram function to this: (literally copy and paste what's below)
function Histogram(str) %Add input argument
%clear %DO NOT USE CLEAR in a function, the benefit of using a function is you don't have to %clear anything :)
clc;
fid = fopen(str); %Use input argument
myimage = fread(fid, [512, 683]); %take off *uint8
fclose(fid);
Read MATLAB's documentation, it is fantastic, and would allow you to see why fread and uint8 don't go together in a matter of seconds (seriously less than 20 seconds would give you your answer) and it would also solve all your other extremely basic issues you are having.
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.