I'm struggling with DataAvailableFcn to receive data from Bluetooth Low Energy device in appdesigner.
Due to example: Work with Device Characteristics and Descriptors
displayCharacteristicData(src,evt) should have two input arguments - src and evt. It works in script file.
However, I cannot call the hanlder function in appdesigner, where all private functions have input argument (app)
Example:
function results = func(app)
end
I've tried
function displayCharacteristicData(app,src,evt)
[data,timestamp] = read(src,'oldest');
disp(data);
disp(timestamp);
end
and
function displayCharacteristicData(src,evt)
[data,timestamp] = read(src,'oldest');
disp(data);
disp(timestamp);
end
calling it using
c.DataAvailableFcn = #app.displayCharacteristicData
Related
Let's say I have a simulation set up in Simulink, which consists of three blocks: Input (From Workspace block), model calculation (S-function block) and results (Outport).
In my class, I do a lot of preprocessing on the input data, which is not shown here. Then, I point the Input block to use the inputArray, which is a property of my class.
classdef SimulationClass < handle
%SimulationClass Simulate process using model
properties
inputArray double = ones(101, 11)
modelFilename char
simOpts struct
simulationTime double
simulationResultsArray double
end
methods
function obj = SimulationClass(simulationTime)
obj.simulationTime = simulationTime;
end
function prepareSimulation(obj, modelFilename)
obj.modelFilename = modelFilename;
open_system(obj.modelFilename);
set_param([obj.modelFilename '/fromWorkspaceBlock'],...
'VariableName', 'obj.inputArray');
obj.simOpts = simset('SrcWorkspace', 'current');
end
function runSimulation(obj)
[~, ~, obj.simulationResultsArray] = sim(obj.modelFilename,...
obj.simulationTime, obj.simOpts);
end
end
end
I can easily start the simulation run from the command prompt.
mySim = SimulationClass(1:10);
mySim.prepareSimulation('mySimulinkSheet');
mySim.runSimulation;
But, if I play around with some settings (e.g., different solvers) and press Run in Simulink, it doesn't work, since the data is not in the base workspace. I.e., I lose the interactivity with Simulink.
Any ideas on how to resolve this issue? The only workaround I have figured out so far would be to write an additional method (like writePropertiesToBaseWorkspace) which would convert the classes properties into variables, using assignin().
I want to find the Minimum of a function using
[x,fval] = fminsearch(#(param) esm6(param,identi),result(k,1:end-1),options)
now for each Iteration step i want some values that the function 'esm6' calculates to be saved in an Array. I tried the following:
In the first line of the function i wrote
identi.sim.i_optiIter = identi.sim.i_optiIter + 1;
to have an iteration-variable counting the iteration steps of fminsearch. And later to catch the values that I need I used
identi.sim.guete_werte.gew(identi.sim.i_optiIter,:,:) = y_sim;
identi.sim.guete_werte.ungew(identi.sim.i_optiIter,:,:) = y_sim_ungew;
and to make sure that I use the new values of the identi-struct for the next function call, I wrote this at the end of the function:
assignin('base','identi',identi);
Now unfortunatly it doesn't do what I wanted it to do. Can anyone help me with this?
EDIT:
I made another attempt on it, using an Output function. I extendend my Options like this:
options = optimset('Display','iter','MaxIter',3,'OutputFcn',#outfun);
But now the Problem is that i cannot figure out where to put this outfun. The outfun Looks like this:
function stop = outfun(x,optimvalues,state,iteration,y_sim,y_sim_ungew)
stop = false;
if state == 'iter'
guete_werte.gew(iteration,:,:) = y_sim;
guete_werte.ungew(iteration,:,:) = y_sim_ungew;
end
end
Now the Problem with it is, that i can not put it in the file, where i call the fminsearch, because that is a script. If i put the outputfunction into a separate .m-function file, it is not able to Access the variables of the esm6 function. And if I add it to the esm6-function file, matlab can't find the function and says
??? Error using ==> feval Undefined function or method 'outfun' for
input arguments of type 'struct'.
My goal is to get the user's input from a uicontrol text box, do operations on the input and then display the output to another text box. MATLAB gives me the error:
Error using
UnitConverter/lbs2kg
Too many input arguments.
Error in
UnitConverter>#(varargin)app.lbs2kg(varargin{:})
(line 22)
'Callback',#app.lbs2kg,'String',app.inputMass);
Error while evaluating UIControl Callback
Here is my code:
classdef UnitConverter < handle
properties
Figure % Graphics handles
DispInputMass
DispOutputMass
inputMass %Variables/Class Properties
outputMass
end
methods
function app = UnitConverter
% This is the "constructor" for the class
% It runs when an object of this class is created
app.Figure = figure('Name','Unit Converter') ;
app.DispInputMass = uicontrol('Style','edit',...
'Callback',#app.lbs2kg,'String',app.inputMass);
app.DispOutputMass = uicontrol(app.Figure,'Style','edit','Position'...
,[168 100 47 26],'String','kg');
end
function lbs2kg(app,evt)
app.inputMass = get(app.DispInputMass,'string');
app.outputMass = app.inputMass*.453;
set(app.DispOutputMass,'string',app.outputMass);
end
end
end
The callback method actually has 3 inputs - MATLAB is throwing this error because it is trying to send three inputs to your callback which is written to only accept 2. The 3 inputs are (in order): the main object (app), the object sending the event (uicontrol) and the event (matlab.ui.eventdata.ActionData).
You can change the code to the following to get it to work:
function lbs2kg(app, obj, evt)
app.inputMass = get(app.DispInputMass,'string');
app.outputMass = app.inputMass*.453;
set(app.DispOutputMass,'string',app.outputMass);
end
Additionally you can change the first line of the function to the following:
function lbs2kg(varargin)
Breakpoint the code at the first line of the callback and investigate the contents of varargin. For more help about varargin see here (http://www.mathworks.com/help/matlab/ref/varargin.html)
In MATLAB:
I have this independent function which runs optimal in the command line:
function datacollect()
filename = uigetfile('*.txt')
[col] = importdata(fileName)
long = columns(:,1)
lat = columns(:,2)
handles.long = long;
handles.lat = lat;
But when I make a function call inside GUIDE:
datacollect()
I get an error:
??? Reference to non-existent field 'output'.
My guess is that there is a name collision between the handles variable in GUIDE that is being used for saving graphics handles and other data, and the one that your function uses.
Try to rename handles in your function to something else, and run again.
I have a set of code that, depending on how the program is initiated, will either be executed locally or sent to a remote machine for execution. The ideal way I imagine this could work would look something like the following:
line_of_code = 'do_something_or_other();';
if execute_remotely
send_via_udp(line_of_code);
else
eval(line_of_code);
end
The thing is, I know that the eval() function is ridiculously inefficient. On the other hand, if I write out line_of_code in each section of the if block, that opens the door for errors. Is there any other way that I can do this more efficiently than by simply using eval()?
EDIT: After more consideration and some discussion in the comments, I have my doubts that function handles can be transmitted via UDP. I'm therefore updating my answer, instead suggesting the use of the function FUNC2STR to convert the function handle to a string for transmission, then using the function STR2FUNC to convert it back to a function handle again after transmission...
To get around using EVAL, you can use a function handle instead of storing the line of code to be executed in a string:
fcnToEvaluate = #do_something_or_other; %# Get a handle to the function
if execute_remotely
fcnString = func2str(fcnToEvaluate); %# Construct a function name string
%# from the function handle
send_via_udp(fcnString); %# Pass the function name string
else
fcnToEvaluate(); %# Evaluate the function
end
The above assumes that the function do_something_or_other already exists. You can then do something like the following on the remote system:
fcnString = receive_via_udp(); %# Get the function name string
fcnToEvaluate = str2func(fcnString); %# Construct a function handle from
%# the function name string
fcnToEvaluate(); %# Evaluate the function
As long as the code (i.e. m-file) for the function do_something_or_other exists on both the local and remote systems, I think this should work. Note that you could also use FEVAL to evaluate the function name string instead of converting it to a function handle first.
If you need to create a function on the fly, you can initialize fcnToEvaluate as an anonymous function in your code:
fcnToEvaluate = #() disp('Hello World!'); %# Create an anonymous function
And the code to send, receive, and evaluate this should be the same as above.
If you have arguments to pass to your function as well, you can place the function handle and input arguments into a cell array. For example:
fcnToEvaluate = #(x,y) x+y; %# An anonymous function to add 2 values
inArg1 = 2; %# First input argument
inArg2 = 5; %# Second input argument
cellArray = {fcnToEvaluate inArg1 inArg2}; %# Create a cell array
if execute_remotely
cellArray{1} = func2str(cellArray{1}); %# Construct a function name string
%# from the function handle
send_via_udp(cellArray); %# Pass the cell array
else
cellArray{1}(cellArray{2:end}); %# Evaluate the function with the inputs
end
In this case, the code for send_via_udp may have to break the cell array up and send each cell separately. When received, the function name string will again have to be converted back to a function handle using STR2FUNC.