Matlab executables, passing variable [duplicate] - matlab

This question already has answers here:
How can I pass command line arguments to a standalone MATLAB executable running on Linux/Unix?
(3 answers)
Closed 9 years ago.
How do I use deploytool to get an executable file from a .m function and use it?
say, I have a .m names foo, here is the code:
function product = foo(array_a,array_b)
product = array_a.*array_b
end
now I use deploytool to generate a foo.exe, how can I use it with the same workspace vars, AKA array_a and array_b?
Regards

I got your code to work by just supplying the executable file with variables.
I first ran mbuild -setup. I have your file, called foo2.m:
function product = foo(array_a,array_b)
if ischar(array_a)
array_a = str2num(array_a);
end
if ischar(array_b)
array_b = str2num(array_b);
end
product = array_a.*array_b
end
The only difference is I ensured that the input are processed as numbers, not strings. Then, I compile:
mcc -mv -R -singleCompThread -N -p optim -p stats foo2.m
(A good explanation of this command is here: MCC example. I used the link to help me get it working.)
Then, just execute the function.
./run_foo2.sh /usr/local/MATLAB/R2011a/ 1 2
....
product =
2
Make sure you specify the location of the compiler libraries as the first argument, then array_a and array_b as the 2nd and 3rd arguments.
I first got an error when I tried to run the executable: error while loading shared libraries: libmwmclmcrrt.so.7.15: cannot open shared object file. I fixed this by finding the library file path (using find . -name "libmwmclmcrrt.so*"). I then corrected the library path I was supplying as the first argument when I called the executable.

You can use eval to convert strings to other data types, such as arrays. See here for more details.
Also, pcode could be another way, if you want to protect your source code.

Related

How to run run mcc in a loop in Matlab

I need help to use mcc -mv in a for loop.
Specifically, I have matlab files names as Myfiles_k.m where k runs from 1:n.
I want to do something like the following
for i=1:n
fname = ['Myfiles_',num2str(i),'.m'];
mcc -mv fname
end
This leads to the following error:
Could not determine type of the MATLAB file 'fname'. Please make sure that you are compiling MATLAB Program files.
Could anyone help with letting me know what am I doing wrong here?
The command
mcc -mv fname
is interpreted as
mcc('-mv','fname')
That is, the arguments are seen as strings. You have a variable containing a string you want to pass to mcc, this requires:
mcc('-mv',fname)

Matlab: set current working directory as top priority in path

In one of my projects, I have a Matlab function called eom.m. When I try to call it, I get errors. I have realised this is because Matlab calls a simulink file, eom.slx, instead, which is in one of the toolboxes.
I would prefer not to rename the function, so I was wondering how I could change the order in the Matlab path so that the folder I call Matlab from always has top priority. That is to say how I can ensure that the files in my current working directory are always those that are called in fact.
Thank you for the help!
You can do it programmatically using addpath with the '-begin' option.
You can use command syntax:
addpath c:/path/you/want -begin
Enclose with quotes if the path contains spaces:
addpath 'c:/path /you/ want' -begin
Alternatively, you can use function syntax:
addpath('c:/path/you/want', '-begin')
This allows having the path stored in a variable:
p = 'c:/path/you/want';
addpath(p, '-begin')

run script command in Matlab

I am running a script in another directory.
Assume I have the following code:
arr = [10;20;30];
run(script); % script= path to the script file + scriptfile.mat ..
x = arr(2);
It gave me the following error:
Undefined function 'arr' for input arguments of type 'double'.
After debugging the code, I found that run(script) .. run the script and then clear all variables .. such as arr.
Is there any way to make run command doesn't clear all variables..
Edit: the following is the original code..
xValues =[2;4;6];
yValues =[10;15;20;30;40];
for var1 =1:size(xValues,1)
results =[];
for var2 =1: size(yValues,1)
run(strcat('C:\Users\as\Desktop\study',num2str(xValues(var1)),'folder\',num2str(yValues(var2)),'folder\file1.m'));
results(var2,1) = yValues(var2);
end
end
Thanks,
Here is an example, erase clear command in your script file:
testing.m
arr = [10;20;30];
run('ScriptFile.m')
x = arr(2);
ScriptFile.m
disp('Hello World');
Command Window
Hello World
After the implementation x holds number 20.
It is clear all inside your script doing the mess I believe.
As for how to avoid it:
Simply remove it. This is generally the easiest and best solution.
Write file to the disk, run script, load from disk. You need a hardcoded file name.
Create "data_holding_function" that has persistent cell array, you load your data there and then restore it after script. You can make the function perform both loading (when you have some input) and unloading (when you don't).

How to Use fullfile in Matlab?

I am studying Suever's answer where I do not understand its application with fullfile in Code 1.
Code 1
filename=strcat('/Users/masi/Images/', 'p.1');
save(fullfile(filename,'.mat'),'time');
saveas(her, fullfile(filename,'.png'));
Output
Error using save
Cannot create '.mat' because '/Users/masi/Images/p.1' does not
exist.
Code 2
filename=strcat('/Users/masi/Images/', 'p.1');
save(strcat(filename,'.mat'),'time');
saveas(her, strcat(filename,'.png'));
Success!
Change made based on Daniel's answer
filenameMat=fullfile('/Users/masi/Images/', 'p.1', '.mat');
save(filenameMat,'time');
but still getting
Error using save
Cannot create '.mat' because '/Users/masi/Images/p.1.mat' does not
exist.
I do not understand.
Why is code 1 giving the error?
You are using fullfile for a wrong application. The documentation clearly explains the parameters:
Folder and file names, specified as strings and cell arrays of strings.
You input a filename without extension and a file extension, hat is not what fullfile is made for. It will insert a file separator between both:
>> fullfile('foo','.bar')
ans =
foo\.bar
fullfile would be the right function to construct filename.

undefined variable or function

I have created a function in MATLAB & have saved it as an m file. When I run my function, it's fine. However using the Windows 7 scheduler it goes to run my function and gives the error message 'Undefined variable 'myMethod' or function 'myMethod.m'.
When I run the which('myMethod.m') it returns the correct folder so not sure what this error message is about?
The pwd method returns the correct address of where my function is too, C:\SomeFolder\MATLAB\Me
Probably its simply not finding the function because it is not on the path.
Assuming you can run builtin functions via the scheduler, try something like this:
p = path
save p
% save c:\ p
In case you cannot even find the saved file, use the last line instead.
Match the path with your files location and presumably the path does not contain the folder which holds your file.