mcc function can't return value,why? - matlab

I use matlab mcc to create a standalone application exe file, then I use php to call the exe file. but I can't get the function return value,it's always empty!! here is my test example in m file
function result=mysum(in)
if nargin<1
in=[1,2,3];
else
in=str2num(in);
end
result=sum(in);
end
then I use the command mcc -m mysum.m to create exe file(I have already configured the matlab compiler).
here is the php file
<html>
<head>
<title>test</title>
</head>
<body>
<?php
exec('F:\myevm\apache\htdocs\shs.exe [2,2,3,3,3] [4,4,4,4,4] 356 1567 1678',$ars);
echo '<br>';
echo $ars[0];
?>
</body>
</script>
</html>
however ,the $ars[0] is always empty!!
I tried to find answer by myself or through the Internet,but failed . give me a help, thanks.

Note two things:
You have your function set up to accept a single input argument.
When you run an application from the Windows command line, arguments are passed in as strings.
So if you type mysum 1 (either in MATLAB on the uncompiled program, and I would guess also if you do this from the Windows command line on the compiled program, although I haven't tested this) it will work, giving the answer 1, and if you type mysum [1,2] it will work, giving the answer 3. Note that mysum [1,2] is different from mysum([1,2]), as it is being passed the string '[1,2]', not the array of doubles [1,2].
But if you type mysum 1 2 it will fail, as you are now passing two string input arguments in, and your function is set up to only accept one.
Rewrite your function so that it accepts a variable number of input arguments (take a look at varargin to achieve that), applies str2num in turn to each of the inputs (which will be varargin{1} to varargin{n} if you've used varargin), and then sums them individually.

Related

MATLAB is saying th function is undefined

I am writing a script to access a function that has been written in another script.
When I run the second script the error is that the function is undefined.
I have been working backwards and am currently trying to get the function to work in the command window.
The function file has appeared in the current folder window. When it is highlighted all functions and parameters are displayed in the window below (displays the file name on top then the file contents).
I am still getting a function is undefined when I copy and paste the functions call from the script into the command window.
I tried rebuilding the functions individually in separate scripts, but I am still receiving an error message.
I have made sure the are in the same folder, and are spelled exactly the same, what am I doing wrong?
'''
%file name Lab_5_functions.m
function[vel] = velocity (g,m,co_d,t)
vel= ((g*m)/co_d)^(1/2)*tanh(((g*co_d)/m)^(1/2)*t);
end
function [dvel]= dvelocity (g,m,co_d,t)
dvel=(((.5*(g*m)/co_d)^(1/2)*tanh(((g*co_d)/m).^(1/2)*t_sec))-(((g*t)/(2*m))*(sech(((g*co_d)./m).^(1/2)*t))));
end
'''
v=velocity(1,2,3,4)
%error message below:
Undefined function or variable 'velocity'.
'''
Thanks
-MK
Matlab is searching for functions using filenames. So you define a single public function myfunc in a file myfunc.m.
You can define additional functions in that file, but they will not be accessible outside that .m file.
MATLAB looks for filenames to find the functions and expects the first line of that file to be a function definition.
For example: myfunc.m
function output = myfunc(input)
If you do want many functions in one file (like a module/library), I have used a work-around before: write all your functions in the file, then include an if-else block to call the correct function. Multiple arguments can be parsed with some simple checks (see nargin function). It is a less elegant solution; I only use it if I have many simple functions and it would be plain annoying to have heaps of .m files.
Here is a simple example:
Call the file: myfunc.m
function output = myfunc(fn, arg1, arg2, ...)
function out = func1(arg1, arg2, ...)
out = 0
if strcmp(fn, 'func1')
if nargin == 2
output = func1(arg1)
end
elseif strcmp(fn, 'func2')
...
end

Puzzling error with script run in function

I'm experiencing a puzzling error in Matlab R2012b. It seems that variable names that are also data types exhibit strange behavior. Please see this small example:
function [] = test1()
dataset = 1;
if dataset ~= 0
disp hello
end
end
A call to test1() produces output hello, as expected.
Now, rather than set the value of dataset in my function, I run a script instead.
function [] = test2()
myscript;
if dataset ~= 0
disp hello
end
end
where myscript.m has one line:
dataset=1;
Now, when I call test2() I get this error:
Undefined function 'ne' for input arguments of type 'dataset'.
Error in test2 (line 4)
if dataset ~= 0
(Forgive the variable named dataset - I know that it is also the name of a data type, and it came in the code I was running.) So it seems as if in test2, Matlab creates an empty dataset object rather than using the variable named dataset. Furthermore, this behavior only appears when I set the value in a script rather than in the function body. Even more weird, is that I can do:
>> dbstop in test2 at 4 % line of if statement
>> test2()
K>> dataset
dataset =
1.00
K>> dataset ~= 0
ans =
1
K>> if dataset ~= 0, disp hello; end
hello
K>> dbcont
and I get the same error! The error is not displayed in debugging mode but it is in normal execution.
Can anyone reproduce this? What is going on here?
The MATLAB online help has some pages dealing with this issue; Variables Names and Loading Variables within a Function seem to be the most relevant.
There is no explicit page that discusses how MATLAB resolves names at compilation time, but there is one little tidbit at the bottom of the Variables Names page: "In some cases, load or eval add variables that have the same names as functions. Unless these variables are in the function workspace before the call to load or eval, the MATLAB parser interprets the variable names as function names."
In other words, if the parser finds an explicit assignment to a variable whose name is the same as another existent object, the local definition takes precedence.
In your test2(), there is no explicit assignment to a variable dataset; therefore, when the file is compiled, the parser interprets dataset to be a class constructor (since the parser will not run or inline myscript into the function).
Then at run-time, even though a variable named dataset has been poofed1 into the function's workspace, the interpreted code that is running still has the dataset symbol in the if-statement associated with the class constructor.
If you need to, you can still use the dataset variable name and load from an external file, but it should be done with an explicit assignment via a function call. For example:
dataset = initialize();
Now the parser will notice that dataset is some arbitrary output of the function initialize and all will be well. In fact, you can have even have initialize return a dataset constructor to the dataset variable if you wanted.
1 When variables are defined without explicit assignment, MATLAB people (at least on some of their blogs I've read) called this 'poofing'. Using load without any output arguments, using eval, and simply running scripts (not functions) can all poof variables into the workspace. This can work fine as long as the variable names do not conflict with other in-use symbols at compile time.

MATLAB function output ans unwanted

I have a function
function [ obsTime, obsWDIR, obsWSPD, obsSWH, obsMWD ] = readObsC(obsFile, endTime)
that when I run it, it gives an output of a huge array ans, which is the same array as obsTime. But obsTime, obsWDIR, obsWSPD, etc. don't display. Not a single line of code is supposed to display ans.
When I'm in debugging mode, I run the code and stop it at the very last line, and it doesn't give an output ans. Only when I hit 'step' twice and the function ends, does the ans output appear.
Everything in the function has semicolons.
Why does ans appear? Where are my other outputs?
In your function definition, you name the formal input and output arguments. That determines the name which these arguments will use within the function.
The function has its own environment, and variable names inside the function are completely independent of variable names outside the function, unless you use global or evalin('caller').
You have to provide actual input and output arguments at the time of the call, which determines how the code outside the function refers to those same arguments. There is no automatic passing of arguments simply because the names match! The only automatic thing is that if you don't specify the actual output arguments, the first actual output argument will be ans and the rest are discarded.
You could have figured this out if you simply read the MATLAB documentation for ans:
The MATLABĀ® software creates the ans variable automatically when you specify no output argument.
The function declaration specifies the return values, but when you call it, you don't specify anywhere for the output to go. When you call something on the command line, the output is always defaulted to ans unless you assign a variable to the output of the function when you call it.
I defined a simple function called myfunc as:
function [one,two,three,four] = myfunc(value1,value2)
Ex, using workspace variables (denoted ws_) to capture function output:
>> [ws_one,ws_two,ws_three,ws_four] = myfunc(1,2)
prints:
ws_one =
1
ws_two =
2
ws_three =
1
ws_four =
2

Matlab executables, passing variable [duplicate]

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.

Accessing variables from a .m file

I run a program, which is a function - here I'll call it 'myfxn' - that outputs several different variables. But when I try to access the data I get
??? Undefined function or variable 'myfxn'.
How do I access the data? Thanks for your help.
Your question is a bit confusing - you claim you run the function, but then you also say that Matlab throws an error indicating it can't run the function.
Here's two things to test
Is myfxn on the Matlab path? Run the command which myfxn. If that does not find the function, change directory (using cd or the directory browser on the Matlab Desktop) to where myfxn is located.
Does the function actually generate output? If it's a function, the first line should look something like this: function [out1,out2] = myfxn(in1,in2), where in1 and in2 are two input arguments, and out1 and out2 are output arguments. Then, you could call myfxn like this: [a,b] = myfxn(2,'something');, and it will use the two inputs to generate the two outputs, which are assigned to a, and b, respectively.