Wants to execute matlab file with php

조회 수: 24 (최근 30일)
Pramod Amrutkar
Pramod Amrutkar 2017년 12월 27일
댓글: Walter Roberson 2018년 10월 24일
how to pass php dynamic file path to matlab(any function where I can execute matlab file code). Want to show the output on browser. SO, how can I code for this.

채택된 답변

Kojiro Saito
Kojiro Saito 2017년 12월 30일
exec or system commands of PHP with matlab -r would be an answer to call MATLAB scripts from PHP. Detail of the integration depends on your MATLAB scripts, but there's no MATLAB interface for PHP, so you need to pass MATLAB calculation results to PHP via File I/O in the following simple example, result of magic square is written in result.csv and will be loaded in PHP and shown in Web browser. matlab command line options are different among Windows, Linux and Mac, for example, -nodesktop option is available in Linux, but for Windows, -automation option is available. So, MATLAB command line string in PHP will be different depending on OS.
magicSquare.m
function out = magicSquare(n)
if ischar(n)
n = str2num(n);
end
out = magic(n);
csvwrite('result.csv', out);
sample.php (Linux version)
<!DOCTYPE html>
<html>
<head>
<title>PHP Test</title>
<meta charset="utf-8">
</head>
<body>
<?php
// Get current working directory
// magicSquare.m exists in this directory
$pwd = getcwd();
// Set command. Please use -r option and remember to add exit in the last
$cmd = '/usr/local/MATLAB/R2017b/bin/matlab -nosplash -nodesktop -sd ' . $pwd . ' -r "magicSquare(5);exit" -logfile log.txt';
// exec
$last_line = exec($cmd, $output, $retval);
if ($retval == 0){
// Read from CSV file which MATLAB has created
$lines = file('result.csv');
echo '<p>Results:<br>';
foreach($lines as $line)
{
echo $line.'<br>';
}
echo '</p>';
} else {
// When command failed
echo '<p>Failed</p>';
}
?>
</body>
</html>
sample.php (Windows version)
<!DOCTYPE html>
<html>
<head>
<title>PHP Test</title>
<meta charset="utf-8">
</head>
<body>
<?php
// Get current working directory
// magicSquare.m exists in this directory
$pwd = getcwd();
// Set command. Please use -r option and remember to add exit in the last
$cmd = 'C:\MATLAB\R2017b\bin\matlab -automation -sd ' . $pwd . ' -r "magicSquare(5);exit" -wait -logfile log.txt';
// exec
$last_line = exec($cmd, $output, $retval);
if ($retval == 0){
// Read from CSV file which MATLAB has created
$lines = file('result.csv');
echo '<p>Results:<br>';
foreach($lines as $line)
{
echo $line.'<br>';
}
echo '</p>';
} else {
// When command failed
echo '<p>Failed</p>';
}
?>
</body>
</html>
Here's result of PHP shown in web browser.
Hope this help.
  댓글 수: 1
Pramod Amrutkar
Pramod Amrutkar 2018년 1월 4일
First of all big thanks Kojiro Saito for answering my question. I want it without function is this possible. Means you have passed parameterised function but I want it without function. I want to pass direct filename in $cmd command

댓글을 달려면 로그인하십시오.

추가 답변 (5개)

Arijit Nandi
Arijit Nandi 2018년 2월 18일
$cmd = 'C:\MATLAB\R2017b\bin\matlab -automation -sd ' . $pwd . ' -r "magicSquare(5);exit" -wait -logfile log.txt';
please explain this Kojiro Saito . I am new in matlab.
  댓글 수: 4
Kojiro Saito
Kojiro Saito 2018년 2월 21일
Do you mean you have created a stand alone application by compiling with MATLAB Compiler? If so, you would have created magicSquare.exe. Put this exe in PHP current directory, and change the command line description to .
$cmd = 'magicSquare.exe 5';
in PHP script.
Walter Roberson
Walter Roberson 2018년 10월 24일
Note that using MATLAB Compiler does not speed up execution. The exe produced use the same execution engine as interactive MATLAB does.
MATLAB Coder (a different product) generates C/C++ code that can potentially be faster, if you are using the subset of MATLAB that it supports (and in some highly vectorized cases, MATLAB itself is more efficient than the generated C/C++ code, which might not be using the high performance libraries.)
If you need the full support of MATLAB Compiler but need the startup time to be reduced then the appropriate product is MATLAB Production Server.

댓글을 달려면 로그인하십시오.


Samreen Akhtar
Samreen Akhtar 2018년 4월 5일
C:\MATLAB\R2017b\bin\matlab

Samreen Akhtar
Samreen Akhtar 2018년 4월 5일
what does it mean
  댓글 수: 4
Kojiro Saito
Kojiro Saito 2018년 4월 10일
You need to pass the output image to php and php needs to create img tag. Simple way is as follows.
showDise.m
function showDise( )
fig = figure('visible', 'off');
a=imread('dise.jpg');
imshow(a);
h=imdistline();
api = iptgetapi(h);
saveas(fig, 'result.jpg')
end
sample.php
<!DOCTYPE html>
<html>
<head>
<title>PHP Test</title>
<meta charset="utf-8">
</head>
<body>
<?php
$pwd = getcwd();
$cmd = 'C:\MATLAB\R2017b\bin\matlab.exe -automation -sd ' . $pwd . ' -r "showDise;exit" -wait -logfile log.txt';
exec($cmd, $output, $retval);
if ($retval == 0){
// Read from image file which MATLAB has created and return img tag
echo '<img src="result.jpg"/>';
} else {
// When command failed
echo '<p>Failed</p>';
}
?>
</body>
</html>
Please replace "C:\MATLAB\R2017b\bin\matlab.exe" in $cmd with your MATLAB installation path.
Nazia Hameed
Nazia Hameed 2018년 10월 24일
did u manage to call matlab using php ?

댓글을 달려면 로그인하십시오.


Fatima Kahoot
Fatima Kahoot 2018년 4월 25일
i have created an exe file of matlab standalone application and wants to execute it with php using exec command and wants to show the output that is created by matlab .exe file in web where user will interactively change the distance of imdistline and then save the distance retrieved using api.getdistance in database. i am new in matlab and does not know how it can be done. Please help me in this regard and please tell me that where is to place Matlab Compiler Runtime? i have also attached my code of php and matlab.
  댓글 수: 1
Kojiro Saito
Kojiro Saito 2018년 4월 26일
I'm not sure what is needed for input and what output would be from ObjectSizeCalculator.exe.
If the output is csv or text file, for example, result.csv, the PHP codes might be similar to the following.
$lines = file('result.csv');
echo '<p>Results:<br>';
foreach($lines as $line)
{
echo $line.'<br>';
}
echo '</p>';
Also, MATLAB Runtime should be installed in the server where PHP is running.

댓글을 달려면 로그인하십시오.


Mario Jose
Mario Jose 2018년 8월 16일
Hi, I'm new to matlab and I'd like to know if someone helps me. I want to run a matlab file, but you have to pass a route, I think it can be the concatenation, the route receives it by parameter the file of mattab
<!DOCTYPE html>
<html>
<head>
<title>PHP Test</title>
<meta charset="utf-8">
</head>
<body>
<?php
$pwd = getcwd ();
$ruta='D:\Tesis2.0\images\CPT-792_2.jpg';
$cmd = 'C:\Program Files\MATLAB\R2017a\bin\matlab.exe -automation -sd ' . $pwd . ' -r "D:\Tesis2.0\prueba2('$ruta');quit" -wait -logfile log.txt';
exec($cmd, $output, $retval);
if ($retval == 0){
$c = file_get_contents('D:\Tesis2.0\placa.txt');
echo $c;
} else {
// When command failed
echo '<p>Failed</p>';
}
?>
</body>
</html>

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by