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 Center および File ExchangeWeb Services についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by