how to connect matlab with php?

조회 수: 2 (최근 30일)
Jeneffir Jeneffir
Jeneffir Jeneffir 2021년 9월 13일
답변: Akanksha 2025년 2월 17일
hello, i want to connect php with matlab and i don't know how to do it i hope someone help me with simple example , thank you.

답변 (1개)

Akanksha
Akanksha 2025년 2월 17일
To call MATLAB scripts from PHP, you can use PHP's exec or system commands with the matlab -r option.
While there isn't a direct MATLAB interface for PHP, you can pass MATLAB calculation results to PHP through File I/O.
Refer to following example where the result of "magicsquare.m" is written in "result.csv" and will be loaded in PHP and shown in Web browser.
magicSquare.m -
function out = magicSquare(n)
if ischar(n)
n = str2num(n);
end
out = magic(n);
csvwrite('result.csv', out);
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>
Hope this helps!

카테고리

Help CenterFile Exchange에서 Web Services에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by