How to change LabVIEW Control value (in already running VI) via Active X? - matlab

I am trying set a control value in my VI (which is already running).
I use the following commands:
e=actxserver('LabVIEW.Application');
vipath='C:\DATA\Labview\test.vi';
vi=invoke(e,'GetVIReference',vipath);
% my control parameter is z which is DBL (double precision)
vi.SetControlValue('z',10)
Everything seems to work fine ! I can see the 'z' value change to 10 in VI but actually VI is not reading that value and VI application is not responding to this value
The VI is a third party application, which is developed by someone else. Unfortunately, I don't have privilege in this forum to post a picture of it (I need 10 points)
I am trying to control variable "z" from Matlab. The "z"is an input to a .dll file. Note that I can do it by front panel control in VI, but can't using Matlab as described earlier.

Without seeing the code, it's impossible to tell, but here are a couple of guesses:
Are you reading the control using a value change event? This event is only triggered either by a change in the UI or by calling the Value(Signaling) property for the control. I'm not sure if you can access this property from the ActiveX interface, but you can try by getting a reference to the control (although I have no idea how that's done from the ActiveX interface either. Maybe the VI has a method?). If you can't, the best would probably be to change the VI to poll the control. You could also have another loop which will fire the event whenever the control changes, but if you do that, I would suggest you have a separate control just for passing that value.
Another possibility - the value is read from the control's terminal before you modified it and your code uses the value on the wire, not the value from the control.
If that doesn't help, post the code.

Got it ! The control variable 'z' was within a event structure, and it was set to value change of 'z'. So, I just removed it for the time being and it works well.
However, I am not sure how to work with an event structure to control it using Matlab active x

Related

Libnodave on value changed

I am using libnodave 0.8.4.4 library to connect to a S7 PLC and what I would like to know if how can I detect if a bit (e.g. DB100.DBX8.0) in PLC DB changes its value. What I did is to read this bit within a while loop but I would like to create an event on value changed on this bit and launch a task when it happens.
There is no default event available with libnodave or any other libraries like S7.net.
Either you need to use OPCor write your own function which will read the set of bits set on time and notify to the main program.

My simple Stateflow model shows error

I'm almost new to Simulink and Stateflow. I am trying to model some simple state machines. But when trying to run the model, it shows errors (on the main Matlab screen).
Warning: Input port 1 of 'sample/If' is not connected.
Warning: Output port 1 of 'sample/If' is not connected.
Parsing failed for machine: "sample"(#90)
1) Is there anything wrong with this?
2) One more question: How can I add a timer on S2? e.g. we can not stay more than 2 minutes on S2. So as soon as we enter S2, a timer starts, and when it reaches 2 minutes, then should transition to S3.
P.S. For some reasons, the stateflow thumbnail on simulink scheme is not showing the updated model; there is no condition and if_outfput variables anymore!
Well, the error message is pretty self-explanatory: you haven't connected the input Condition of your Stateflow chart to anything, hence the error. Connect it to a signal in your Simulink (whatever represents your condition signal). Likewise, you haven't connected the output of the chart to anything either. You say these variables aren't there anymore, but Stateflow doesn't seem to think so. Have you deleted them from the Model Explorer? If not, Stateflow will still think they're part of the chart. See Use the Model Explorer with Stateflow Objects in the documentation for more details.
For the timer, yes it's possible. At the moment, you exit S2 to S3 when input ==1. You can change the transition to be [input == 1] || after(2000,sec) (I think). You may need to enable support for absolute time in the model configuration parameters. See Control Chart Execution Using Temporal Logic in the documentation for more details.

Plotedit state of a Matlab figure

How can I extract the plotedit state of a Matlab figure inside a function? If I want to know the zoom state of the current figure, I can write:
zoomState = get(zoom(gcf), 'Enable');
A similar syntax for plotedit does not work, since plotedit(gcf) toggles the plotedit state without returning anything. Without having a way to get plotedit's current state, I have no clue how to temporarily put it to 'off' and restore its value once my function has finished. Any ideas?
I just received an answer from the MathWorks on this issue:
here is an undocumented feature that you might want to use :
ison = plotedit(gcf,'isactive')
This will tell you if PLOTEDIT is active or not. However, since this
is undocumented it might change or not work in future releases.
I think that this answers my question.
If you type open plotedit at the command line, you will see that it is actually an m-file. When the state is toggled, it calls the undocumented function activateuimode to do the dirty work. Taking a wild guess, I typed help getuimode at the command line and got back the message
This function is undocumented and will change in a future release
So the function exists. Presumably, calling it with the same input arguments as activateuimode in plotedit will do what you want. That is,
getuimode(myFigHandle, 'Standard.EditPlot')
On my system, it returns [] if not in edit mode, and an instance of uitools.uimode if it is.
Note, however, that this approach may be a bit dodgy - as the help says, it will probably change in a future release. If you open getuimode, you will see it has been the same since 2007, but as I understand it there has been a major overhaul of the UI system in R2013, so it may have changed in the most recent release (I'm running R2012a).

Matplotlib window class? OO equivalent of matplotlib.pyplot.close()?

In Matplotlib:
I can write matplotlib.pyplot.cla() as axes.clear(), where axes is an matplotlib.axes.Axes object.
I can write matplotlib.pyplot.clf() as fig.clear(), where fig is a matplotlib.figure.Figure object.
Then there is matplotlib.pyplot.close(). This closes a window. Does this window correspond to any class? Is there a x.close() that is equivalent to matplotlib.pyplot.close() but in a more object oriented style?
The source code for matplotlib.pyplot.close() for my version is as follows:
if len(args)==0:
figManager = _pylab_helpers.Gcf.get_active()
if figManager is None: return
else:
_pylab_helpers.Gcf.destroy(figManager.num)
elif len(args)==1:
arg = args[0]
if arg=='all':
_pylab_helpers.Gcf.destroy_all()
elif isinstance(arg, int):
_pylab_helpers.Gcf.destroy(arg)
elif is_string_like(arg):
allLabels = get_figlabels()
if arg in allLabels:
num = get_fignums()[allLabels.index(arg)]
_pylab_helpers.Gcf.destroy(num)
elif isinstance(arg, Figure):
_pylab_helpers.Gcf.destroy_fig(arg)
else:
raise TypeError('Unrecognized argument type %s to close'%type(arg))
else:
raise TypeError('close takes 0 or 1 arguments')
So, it appears to be based on the staticmethod
matplotlib._pylab_helpers.Gcf.destroy which does a variety of things I don't quite follow, noting that In the interactive backends, this is bound to the window "destroy" and "delete" events. But I'm not sure how figures and windows are related.
Perhaps I'm wasting my time even thinking about this.
Gcf is a global singleton object that keeps a list of the figures you have opened with plt.figure.
It is essentially a dictionary with some fancy code around it to keep things consistent and keep track of the active figure, ect. The class that is used as a layer between Figure (which is what is returned by plt.figure) and the GUI layer is FigureManagerBase (and it's per-toolkit sub-classes) which is defined in backend_bases.py.
Which sub-class you are using is determined by which backend you are using. These sub-classes deal with things like making sure the figure gets deleted when you click the 'x' button, and the system level gui call-backs.
FigureManagers have a destroy method which triggers their internal tear down. In principle, if you have a Figure object and want to destroy it by hand, you just need to get a hold of the manager. A reference to it isn't stored in the Figure object, but is stored in the Canvas object so you can destroy a window via
fig.canvas.manager.destroy()
which will tear the figure down, however if you do this, it might not properly clean up Gcf (which will retains a reference to the figure manager) which will make future figures made with pyplot have unexpected numbers (it won't go back to one), gca will not work as exected, ect (this is the case with Qt4).
If you are going to use pyplot at all (and be sure it will always work on every backend), then you either need to
directly talk to Gcf (it's in _pyplot_helpers.py and really isn't too complicated once you see what it is trying to do
reach and talk to the GUI directly (see the code in lib/matplotlib/tests/test_backend_qt4.py for how to do with with qt4, but this is going to be very toolkit dependent)
plt.close(fig.number)

MATLAB: Perform callback on variable change?

I am trying to build a GUI with several tabs, using uitabpanel() found at http://www.mathworks.com/matlabcentral/fileexchange/11546 . I would like to resize the GUI based on the currently opened tab; this is available with uitabpanel.SelectedItem.
Basically I would like to build a callback in order to monitor uitabpanel.SelectedItem - when this variable changes, the GUI window should resize appropriately. Generally speaking, I am looking for a way to monitor a variable and execute a callback when the variable changes value.
Is this possible? How would I go about doing this?
Thanks!
I don't have a MATLAB in front of me right now, but if it is implemented as a property -- and as far as I can tell from a quick look at the code, it is -- you can use addlistener function and provide a callback function for it.
addlistener(hTab,'SelectedItem','PostSet',#(s,e)disp('SelectedItem changed'))
I'm not familiar with uitab from the file exchange. However, if it's build upon the built-in uitab, then there should be a selectionChangeCallback or selectionChangeFcn property (depending on your Matlab version). Specify a function for this callback property, and you have a way to execute a function whenever the selection changes.
If that's not possible, the only other way to monitor a variable change (if you can't somehow use objects and set methods) is to use a TIMER OBJECT that periodically polls the value of the variable.
EDIT Since the FEX uitab is based on uipanel, the callback you're looking for is most likely ButtonDownFcn. Before you change it, make sure that it's not used by the uitab function, otherwise, you will want to edit that function.