Matlab GUI callback error - matlab

i have a really strange error... i get the following:
SWITCH expression must be a scalar or string constant.
Error in RL_Bsp>WechselStatus (line 387)
switch GewichtungNutzer
Error in RL_Bsp>togglebutton1_Callback (line 151)
WechselStatus(Status, Aktion, ButtonWert);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in RL_Bsp (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>#(hObject,eventdata)RL_Bsp('togglebutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
The funny part is that i changed nothing, after i restarted my pc earlier it just started to happen where it used to work 10h ago!
The errors seem to be here:
function WechselStatus(Status, Aktion, ButtonWert)
GewichtungNutzer = getappdata(0,'Wert');
global R_Alg
global Ziel
switch GewichtungNutzer
case {'100'}
GewichtungNutzer = 100;
case {'200'}
GewichtungNutzer = 200;
case {'300'}
GewichtungNutzer = 300;
case {'Ziel mit 500'}
GewichtungNutzer = 0;
otherwise
GewichtungNutzer = -1;
end
if get(ButtonWert,'value') == 1
set(ButtonWert,'Backgroundcolor','0.76, 0.87, 0.78');
if GewichtungNutzer > 0
R_Alg( R_Alg(:,Aktion)==0, Aktion ) = GewichtungNutzer;
else
R_Alg( R_Alg(:,Aktion)==0, Aktion ) = 500;
Ziel = Aktion;
end
elseif get(ButtonWert,'value') == 0
set(ButtonWert,'Backgroundcolor','0.11, 0.31, 0.21');
R_Alg(:, Aktion) = -1;
end
and here
function togglebutton1_Callback(hObject, eventdata, handles)
Status = 1;
Aktion = 1;
ButtonWert = hObject;
WechselStatus(Status, Aktion, ButtonWert);
i got really no clue why i get the error now i was reading many times its something to do with the path that the code can't read the gui? would appreciate help !!

This line:
GewichtungNutzer = getappdata(0,'Wert');
is using the function getappdata to retrieve the value 'Wert' that has been stored in the graphics object 0. Graphics handle 0 always refers to the root object. In order for this line to function as intended, that value must be added to the root object first using setappdata. If it hasn't been initialized, it will return [], which will give you the error you're seeing when you try to use [] in a switch statement.
I'm guessing that, when you had previously run the code, the value of 'Wert' had been set on the root object and everything ran fine. When you reran the code later, this value was, for whatever reason, not set on the root object. Either this value is set on the root object by some other piece of code that you have to run first, or there is a place in your code where the value is only set under certain conditions that were not met the second time you ran it.

Related

Making connection to hardware using MATLAB

I'm trying to make a connection to hardware (a controller of a moving stage) from Physik Insrumente using MATLAB. However, I have tried using the code that the company provided but did not work, so I have simplified but still doesn't work.
Here is my code:
addpath ( 'C:\Users\Public\PI\PI_MATLAB_Driver_GCS2' );
Controller = PI_GCS_Controller();
controllerSerialNumber = '118011005';
C867 = Controller.ConnectUSB(controllerSerialNumber);
display(C867.qIDN())
I am getting this error message:
PI_MATLAB_Driver_GCS2 loaded successfully.
Error using PI_GCS_Controller/ConnectUSB (line 17)
There is no interface or DLL handle with the given ID
Error in PI_GCS_Controller/ConnectUSB (line 17)
error(szDesc);
Error in PI_GCS_Controller/subsref (line 46)
varargout{1} = feval(fun,c,par{1});
Error in Untitled (line 5)
C867 = Controller.ConnectUSB(controllerSerialNumber);
ConnectUSB is:
function [c] = ConnectUSB(c,szIdentifier)
FunctionName = 'PI_ConnectUSB';
if(any(strcmp(FunctionName,c.dllfunctions)))
if(nargin<2), szIdentifier = blanks(5000); end,
try
[c.ID, szIdentifier] = calllib(c.libalias,FunctionName,szIdentifier);
if(c.ID<0)
iError = GetError(c);
szDesc = TranslateError(c,iError);
error(szDesc);
end
catch
rethrow(lasterror);
end
else
error('%s not found',FunctionName);
end
How can I connect to this controller?

interrupt a TCP-IP callback function in matlab

Recently wrote code that establishes a connection between two instances of matlab. I can send messages through the TCP-IP connection which will execute code. Now I'm trying to setup the code to be interruptible as I would like to start/stop a function through TCP-IP. Problem though is that sending a second command does nothing until the function is completed. Is there a way to interrupt a TCP-IP callback function?
code:
classdef connectcompstogether<handle
properties
serverIP
clientIP
tcpipServer
tcpipClient
Port = 4000;
bsize = 8;
earlystop
end
methods
function gh = connectcompstogether(~)
% gh.serverIP = '127.0.0.1';
gh.serverIP = 'localhost';
gh.clientIP = '0.0.0.0';
end
function SetupServer(gh)
gh.tcpipServer = tcpip(gh.clientIP,gh.Port,'NetworkRole','Server');
set(gh.tcpipServer,'OutputBufferSize',gh.bsize);
fopen(gh.tcpipServer);
display('Established Connection')
end
function SetupClient(gh)
gh.tcpipClient = tcpip(gh.serverIP,gh.Port,'NetworkRole','Client');
set(gh.tcpipClient, 'InputBufferSize',gh.bsize);
set(gh.tcpipClient, 'BytesAvailableFcnCount',8);
set(gh.tcpipClient, 'BytesAvailableFcnMode','byte');
set(gh.tcpipClient, 'BytesAvailableFcn', #(h,e)gh.recmessage(h,e));
fopen(gh.tcpipClient);
display('Established Connection')
end
function CloseClient(gh)
fclose(gh.tcpipClient);
gh.tcpipClient = [];
end
end
methods
function sendmessage(gh,message)
fwrite(gh.tcpipServer,message,'double');
end
function recmessage(gh,h,e)
Message = fread(gh.tcpipClient,gh.bsize/8,'double');
if Message == 444
gh.Funwithnumbers();
elseif Message == 777
gh.earlystop = 1;
end
end
function Funwithnumbers(gh)
x=1;
while true
if x > 5000, break;end
if gh.earlystop == 1,break;end
x = x+1;
display(x)
end
end
end
end
for ease to understand code.
server
Ser = connectcompstogether;
ser.SetupServer();
ser.sendmessage(333);
Client
cli = connectcompstogether;
cli.SetupClient();
Update:
So after going through the web, I have found out based on this post that the tcpip callback cannot be interrupt. The post was in 2017 which means my 2016a version definitely cannot interrupt a callback.
So An update to my question, Is it possible to start a subprocess in matlab to run the function. I just want to use the callback to start code. If I can start a subprocess from the callback. Than I should be able to free up the main process and use tcpip to start/stop a function on a different computer.
Update 2:
So I tried to utilize parallel processing using the 'spmd' command but the problem still persisted.
function recmessage(gh,h,e)
Message = fread(gh.tcpipClient,gh.bsize/8,'double');
spmd
switch labindex
case 1
if Message == 444
gh.Funwithnumbers();
elseif Message == 777
gh.earlystop = 1;
end
end
end
end
You may use a timer object, which is convenient to delay the execution of some function.
t=timer('ExecutionMode','singleShot', 'StartDelay',0, 'TimerFcn',#myCallback);
start(t);
In this case, the StartDelay is 0, so myCallback will be almost immediately added to the queue of tasks to be processed by Matlab. The execution however will start only after the callback to the tcpip object has been completed. It will block the queue once started, however.
You may try something like:
properties
t=timer('ExecutionMode','singleShot', 'StartDelay',0, 'TimerFcn',#myCallback);
end
function tcpipCallback(gh,tcpObj,~)
message=fread(tcpObj,1,'double');
if message==444
if strcmp(get(t,'Running'),'on')
error('The function is running already');
else
set(gh.t,'UserData',false);
start(gh.t);
end
elseif message==777
set(gh.t,'UserData',true);
end
function myCallback(tObj,~)
ii=0;
while ii<5000
if get(tObj,'UserData'),break,end
ii=ii+1;
pause(.0001); %Pause to interrupt the callback; drawnnow might work too; or perhaps this is not needed at all.
end
end

Undefined function or variable "Vk" when using sysic to create an interconnected system

I am attempting to use sysic to create an interconnected system from a number of state space models. However; I keep recieving the same error:
Undefined function or variable "Vk".
Error in sysic (line 212)
[ard,arl,er] = LOCALpass1(Vk);
Error in addOutputWeights (line 62)
sysic
The code used that generates this error is as follows:
systemnames = 'plantModel WControl WError';
inputvar = '[r(4); u(4)]';
outputvar = '[WControl; WError;r- plantModel]';
input_to_WError = '[r-plantModel]';
input_to_WControl = '[u]';
sysoutname = 'instramentedPlant';
cleanupsysic= 'yes';
sysic
This error was caused because input_to_plantModel was not present in the workspace. For every system that is refered to in systemnames there must be a corresponding input_to_X.
The following code runs correctly
systemnames='plantModel wControl wError';
inputvar ='[r(4); u(4)]';
outputvar ='[wControl;wError;r- plantModel]';
input_to_plantModel ='[u]';
input_to_wError ='[r-plantModel]';
input_to_wControl ='[u]';
sysoutname ='instramentedPlant';
cleanupsysic = 'yes';

MATLAB: errorn in butter() command

I wrote the following function:
function [output_signal] = AddDirectivityError (bat_loc_index, butter_deg_vector, sound_matrix)
global chirp_initial_freq ;
global chirp_end_freq;
global sampling_rate;
global num_of_mics;
global sound_signal_length;
for (i=1 : num_of_mics)
normalized_co_freq = (chirp_initial_freq + chirp_end_freq)/ (1.6* sampling_rate);
A=sound_matrix ( i, : ) ;
peak_signal=max(A);
B=find(abs(A)>peak_signal/100);
if (butter_deg_vector(i)==0)
butter_deg_vector(i)=2;
end
[num, den] = butter(butter_deg_vector(i), normalized_co_freq, 'low');// HERE!!!
filtered_signal=filter(num,den, A );
output_signal(i, :)=filtered_signal;
end
This functions runs many-many times without any error. However, when I reach the line: [num, den] = butter ( butter_deg_vector(i), normalized_co_freq, 'low');
And the local variables are: i=3, butter_deg_vector(i)=1, normalized_co_freq=5.625000e-001
MATLAB prompts an error says:
??? Error using ==> buttap Expected N to be integer-valued.
"Error in ==> buttap at 15 validateattributes(n,{'numeric'},{'scalar','integer','positive'},'buttap','N');
Error in ==> butter at 70 [z,p,k] = buttap(n);"
I don't understand why this problem occurs especially in this iteration. Why does this function prompt an error especially in this case?
Try to change the code line for:
[num, den] = butter (round(butter_deg_vector(i)), normalized_co_freq, 'low');

matlab fails deployment in internal code

I want to compile a gui matlab project, but fail because of this error which is not in my code
Subscript indices must either be real positive integers or logicals.
Error in isscript (line 7)
if strcmpi(pth(end-1:end), '.m') && exist(pth, 'file') == 2
Error in matlab.depfun.internal.Schema/move/#(setMembers)setMembers(~isscript(setMembers))
Error in matlab.depfun.internal.Schema>applyMoveFcn (line 987)
keptFiles = fcn(fileList);
Error in matlab.depfun.internal.Schema>#(files,destMap)applyMoveFcn(op,files,destMap,destSet,reason,rule) (line 822)
#(files, destMap)applyMoveFcn(op, files, destMap, ...
Error in matlab.depfun.internal.Schema/applySetRules (line 141)
xformedSet = feval(operations{n}, xformedSet, rMap);
Error in matlab.depfun.internal.Completion/applySetRules (line 1059)
[modifiedList, rMap] = ...
Error in matlab.depfun.internal.Completion/initializeRootSet (line 1142)
[addedFiles, ruleFilter, notes] = ...
Error in matlab.depfun.internal.Completion (line 1601)
obj.RootSet = initializeRootSet(obj, files);
Error in matlab.depfun.internal.requirements (line 166)
c = matlab.depfun.internal.Completion(files, tgt);
Error in appcreate.internal.appbuilder.getDependencyList (line 173)
[dependentfiles, depproducts, ~] = matlab.depfun.internal.requirements(varargin, 'MATLAB');
How can I get matlab (2013b) to compile the package?
The code where matlab fails is (which is NOT my code)
function tf = isscript(files)
% ISSCRIPT Is the file a script file?
tf = false(1,numel(files));
for k=1:numel(files)
pth = files{k};
% Can't be a script if it isn't an M-file.
if strcmpi(pth(end-1:end), '.m') && exist(pth, 'file') == 2
mt = matlab.depfun.internal.cacheMtree(pth);
fcn = mtfind(mt, 'Kind', 'FUNCTION');
tf(k) = isempty(fcn);
end
end
The code will fail for files with a filename of length 1. Either rename all files with such a short filename or change the line to:
if length(pth)>1 && strcmpi(pth(end-1:end), '.m') && exist(pth, 'file') == 2