how to connect matlab with php?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponses (1)
Akanksha
le 17 Fév 2025
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!
0 commentaires
Voir également
Catégories
En savoir plus sur Web Services dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!