Alternative to 'evalin' with MATLAB App Designer - matlab

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!

Related

How to call a Matlab function from Labview

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.

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.

Is there something like a "pre-build" callback function?

I have a Simulink model, the purpose of which is automated code generation.
My model uses S-functions (developed by another party), which has hard-coded assumptions about the path. For instance, several external data files are needed, which are referenced in the S-function via a relative path like ..\Bin\data\datafile.bin. This makes it necessary to set MATLAB's current working directory to a specific path before the model can be run.
I can automatically check and set the correct path via model callback functions. However, all model callback functions only seem to be related to the simulation process, not the build process. That means that I can run the model irrespective of what directory I'm in, but when I try to build the model, it always fails unless I manually navigate MATLAB back to the correct directory.
Needless to say, that's quite annoying. So I was wondering if there is something like a "preBuildFcn" callback fnuction, a function that is run before starting the build process? Any other solution (that does not involve modifying the S-function) is also very welcome.
There are plenty of hooks into the build process of Simulink / Embedded Coder ('entry', 'before_tlc', 'after_tlc', 'before_make', 'after_make', 'exit', and 'error'). I assume you want an 'entry' hook.
All you need to do is write an M-function with the name your_system_target_file name_make_rtw_hook, as explained in the documentation Customize Build Process with STF_make_rtw_hook File.
In case you can't open the online documentation (login required), here is the path to the HTML in your MATLAB installation: MATLAB root\help\rtw\ug\customizing-the-target-build-process-with-the-stf-make-rtw-hook-file.html
I am not sure whether building simulink models is sufficiently similar to building regular MATLAB programs, but here is what I used in the past:
Set up the project manually
Build the project programmatically
The program that is used to build the project should be able to set the path or do other custom things.

running compiled matlab from matlab

I am trying to run compiled MATLAB code (by mcc) from inside MATLAB in a way that I can avoid using another license that is required by the compiled code. We need this because we run this same specific code part again and again and execution is stuck due to license waiting. We don't want to buy tons of this specific license just to mass run the same part. Is there any way to do this? tutorial?
Is it possible to compile a .m file to dll/so and wrap it like a mex and call it from MATLAB on the fly? How would I pass and retrieve complex arguments?
According to
http://www.mathworks.de/products/compiler/description3.html
creating shared libraries should well be possible.
Concerning passing and retrieving complex arguments:
If you plan to use mex, I'd assume you should be able to call the shared-library "main"-function with any arguments you'd like, using the mxArray type that you'll have to use anyway.
To run the MATLAB-compiled code in MATLAB, you want codegen, part of the MATLAB Coder. See this blog post on generating C code from MATLAB. The alternative, deploying code with mcc/mbuild and then reloading it into MATLAB with loadlibrary is rather contorted, and I wouldn't advice it.