How to call a Matlab function from Labview - matlab

I’m struggling to understand how to call a Matlab function from Labview. I’m using Labview 2020 SP1 and I’ve created the following m file (saved to my desktop):
function A = test()
A{1,1}=ones(1,3);
A{1,2}=ones(1,4);
end
In Labview I’ve added a Matlab script node and imported the function. However when I click run I get the following error:
Error occurred while executing script. Error message from server: ??? Error: Function definition not supported in this context.
Functions can only be created as local or nested functions in code files.
I was expecting the function to be able to run but do nothing as I’ve not added any outputs to the Matlab script node.
Can anyone point me to where I’m going wrong? I’ve looked through the Labview examples but they only seem to demonstrate calling a Matlab script not a function.

Firstly the function would need to be in MATLAB's path (which typically wouldn't include the current user's desktop). MATLAB's path is a list of file locations that MATLAB will search in to find a function definition.
You can view/edit the current path by typing pathtool into MATLAB's interactive command window which will bring up a GUI.
Then your MATLAB script node should be able to call the function as below - note I have added an output just to check the result of the call.
This image is a "LabVIEW Snippet" so you should be able to drag and drop it directly into the block diagram of a new VI and then save and run the VI.

Related

Alternative to 'evalin' with MATLAB App Designer

I'm creating an app that I eventually want to compile as a standalone application. The app constructs a GUI for a preexisting MATLAB program and I utilize the evalin function twice in the following manner.
First, I run a 'start' function as follows
evalin("base",'func.start')
This instantiates a number of variables which are necessary to run the GUI.
Next, I run the following code to read in one of the variables into the AppDesigner workspace.
fx = evalin("base",'fx')
I wanted to see if there was a deployable alternative to 'evalin' that could be compiled by the MATLAB Compiler. Any help would be greatly appreciated!

Run Simulink from Matlab function

I am running Simulink using FastRestart, as I need to start and stop the simulation multiple times changing parameters. When I run Simulink from the main script, there are no problems. However, as soon as I make the script a function so that I can run it for different input data, I get an error that is clearly related to Simulink not seeing the Matlab workspace within the function.
To be more precise, say sfile is my Simulink file, then I run the following lines AFTER having initialized all variables I need in structures in Matlab:
load_system(sfile);
set_param(sfile,'FastRestart','on');
set_param(sfile,'SaveFinalState','on');
set_param(sfile,'SaveCompleteFinalSimState','on');
set_param(sfile,'SimulationCommand','update');
At the last line, I get the error that Simulink does not recognize mdl.tStep (which is the time step), as mdl is not a recognized structure. In fact, it is and if I run Simulink from the main script everything is fine.
Now, in the past, I would have used
options = simset('SrcWorkspace','current');
However, an expert I know has advised me against simset (as it may get deprecated in the future) and encouraged me to use set_param instead. I have
looked up the options for set_param on-line, but I could not find the setting for the Matlab workspace.
Any help would be greatly appreciated. Thank you in advance!
In many instances it is better to use the Model Workspace rather than the Base Workspace:
hws = get_param(model, 'modelworkspace');
hws.assignin('mdl',mdl);
At least be aware that this option exists.
A solution to your problem might be to use the assignin-function to all the variable whose value you want to pass to simulink in your matlab base workspace. To do so just use
assignin('base','mdl',mdl)
Having the variable in your base workspace should allow simulink to see it.

Allowing user provided .m functions in a compiled Matlab GUI

I have tried to read many documentations (especially http://blogs.mathworks.com/loren/2008/08/11/path-management-in-deployed-applications/ where the question is asked but not answered to) on compiled Matlab GUI but was unable to find an answer to my question.
I want to create a compiled GUI in Matlab (compiled with deploytool and that can run on a computer that does not have Matlab), where at some point, the user can specify his own matlab .m file (say for example : myProfile.m) and the Gui uses it later on (this last point is the tricky part).
myProfile.m is some simple function (it takes one argument and outputs one value) that can be located wherever the user wants, and that is totally user defined. I give here a simple example:
function [y] = myProfile(x)
y = x^2;
end
but it can be more complex.
In the Gui I ask the user the path to his profile function and I try to make it a function handle :
Button1 = uicontrol('String','Browse path to your Profile',...
'Position',[320 10 150 150],...
'Callback',#button1_Callback);
function [profileFunc] = button1_Callback(varargin)
[ProfileName,ProfilePath] = uigetfile({'*.m'},'Select your profile');
addpath(ProfilePath);
profileFunc = str2func(strcat('#',ProfileName));
% profileFunc will be used later on in the code
end
Of course, after compilation this code doesn't work, and I get the following error:
'C:\Users\...\myProfile.m' is not in the application's expanded CTF archives at
'C:\Users\...\mcrCache8.0\myGui'. This is typically caused by calls to ADDPATH ...
I know that using addpath in a Gui does not work when you compile the Gui. But if I don't add the path, the program can not find the user provided myProfile.m . So how can I solve this?
Thanks,
Sam
When viewing this from a licensing point it is very simple. Mathworks can not allow to deploy such code, you could easily deploy your own command line version of matlab which runs arbitriray code and does not require any license.
In my opinion there is only one way to go: Deploy m-code and ask the user to install matlab or octave.
Alternative:
If you deploy a jar, the JRE is already running. Concider using java script as the JRE already brings the script engine. Then the user would have to input java script.

Cannot use some Matlab MPC Toolbox functions

I'm trying to use for example setoutdist Matlab function from the MPC Toolbox (I'm using Matlab R2013a on Windows 8.1). As a response I'm receiving:
Undefined function 'setoutdist' for input arguments of type 'ss'.
I am able to get help about this function using help setoutdist. When I'm typing the function name and left parenthesis I'm receiving prompt with list of the arguments. When I'm using which setoutdist -all I'm receiving proper output:
C:\Program Files\MATLAB\R2013a\toolbox\mpc\mpc\#mpc\setoutdist.m % mpc method code here
But the function doesn't work even in default Matlab path, so I don't think it is shadowed.
The same is with the other functions, for example setindist, setestim, mpc_struct, etc. but mpc, mpcstate and mpcmove functions works correctly.
I was trying: clear all, clear classess, rehash toolbox, rehash pathreset, rehash toolboxreset, restoring default paths using pathtool. I've blocked the antivirus and added exeptions to it's list. I've even reinstalled my Matlab, nothing helped.
Maybe this is significant: when I'm trying to edit the setoutdist.m I'm receiving message that access is denied.
I will extremly appreciate any help...
How are you calling setoutdist? The correct syntax is one of the three (see documentation - for R2014b):
setoutdist(MPCobj,'integrators')
setoutdist(MPCobj,'remove',channels)
setoutdist(MPCobj,'model',model)
where MPCobj is a Model Predictive Controller object, created for example with the mpc function. It looks like from the error message that you are calling the function with a state-space object, which is not allowed (I'm guessing).

How to call chi2pval from my code

I am trying to use ‘chi2pval’ function in my code but it’s a private function to stats. How can I make it available to me?
chi2pval is a private function that's part of the Stats toolbox. This function does exist in MATLAB, but you aren't able to call it directly as it's located in a private folder that isn't accessible by you... at least not normally. What you can do is search for where the source file is located. You can do that typing in the following command into your Command Prompt:
which chi2pval -all
which determines where a particular MATLAB function you are looking for is located on your machine. The -all flag displays the paths to all functions that you're looking for. On my Mac OS X machine, this is what I get:
/Applications/MATLAB_R2013a.app/toolbox/stats/stats/private/chi2pval.m % Private to stats
You can take a look at the source by invoking edit in front of that string that contains the full path to the function, as well as the function name itself:
edit /Applications/MATLAB_R2013a.app/toolbox/stats/stats/private/chi2pval.m
When I do this, I see the source code of chi2pval.
Now, what you can do from here is if you want to actually call chi2pval, you can copy the M-file from this directory to where you are calling your main code, then go ahead and run your code.
Hope this helps!