2

I successfully made some script to be executed by matlab throught CLI from a web interface of my own. But now, I am trying to get the output of the scripts that we can launch from it.

Does anyone know how to get the values returned by matlab ?

For instance, my script "A.m" is :

a = [3, 6, 9];

I want to get :

a =

     3     6     9

My script "B.m" is :

a = [1 2 3 4 6 4 3 4 5]
b = a + 2
plot(b)
grid on

I want to get the result below + the image generated :

a =

     1     2     3     4     6     4     3     4     5

b =

     3     4     5     6     8     6     5     6     7

I have used these previous topics :

Thanks a lot !

Edit : I call my files this way :

C:\...\matlab\bin> matlab -wait -minimize -nodesktop
 -automation -r "run('C:\...\Source2.m');exit;"
Braiam
  • 1
  • 11
  • 47
  • 78
Charaf
  • 324
  • 3
  • 18
  • why don't you save the results to a mat-file and read the results afterwards? – Shai Jun 24 '13 at 13:56
  • It is because I won't write all the source files by myself. I am not sure the scientists who will post these scripts will always think about doing so. So, I think that just getting the results is the best way. I will display them on my webpage. – Charaf Jun 24 '13 at 14:12
  • 3
    As a scientist, I would be really interested in your full solution (from HTML code, over PHP-code, to Matlab-code)! Should you ever find the time to write a more extensive question-answer pair of the sorts "How can I run Matlab scripts from the web", I think it would be really helpful to the community! (Answering your own question is possible and even encouraged.) – Martin J.H. Jun 24 '13 at 14:30
  • Yes, the final goal is to run it from the web, but in order to fit the needs of a more wide-scaled software. I still have a lot of work to do tought. This is really needed by scientists. – Charaf Jun 24 '13 at 14:38
  • Have you read the MathWorks' "Web Example Guide"? http://www.mathworks.com/help/pdf_doc/compiler/example_guide.pdf – Peter Jun 24 '13 at 14:54
  • Might not be the best solution, but a quick one would be to run your scripts inside `evalc` and save the output from that? Some more details (and possible duplicate) here: http://stackoverflow.com/questions/6654975/redirecting-matlabs-disp-to-a-text-string – Hugh Nolan Jun 24 '13 at 15:55
  • Also, I once wrote a script to override the inbuilt fprintf function to add custom expressions at the start of output from an external toolbox. Something similar might be useful? Briefly tried it with `disp` and `display` for a few minutes but didn't get it working - might be an avenue to investigate though? – Hugh Nolan Jun 24 '13 at 16:00
  • I'm a little confused. How do you use "A.m" and "B.m"? With the command line? Can you show the command? It might be a good idea to just dump all variables into an ASCII file once the script ran and do the visalization yourself. You could: `matlab -nodisplay -nosplash -nodesktop -r "script(); save all.mat"` – ClojureMostly Jun 24 '13 at 19:26
  • Peter : Yes. It shows a solution that needs more actors than what I have. I don't have any Middle Tier actor, nor any Server Admin. All that my plan can provide for now is : 1/ end user 2/ web developper with knowledge in system developpement but little knowledge in matlab 3/ the scientist (aka matlab programmer). I am sure I can make them work well together. The only Java technology my web developper can do is in the context of developping generic code for Apache Cocoon. – Charaf Jun 25 '13 at 14:24
  • Aralo : Thanks for this useful point. I've added the command that I use to the message. `C:\...\matlab\bin> matlab -wait -minimize -nodesktop -automation -r "run('C:\...\Source2.m');exit;"` – Charaf Jun 25 '13 at 14:35
  • Hugh Nolan : Thank you. I am investigating the possibilty to reach my goals by rewriting the I/O matlab functions to make them put their results into files or streams. It seems to be on a good way. – Charaf Jun 25 '13 at 15:10

1 Answers1

0

Since I do not have a copy of matlab I cannot check this but I assume if you run it using command line interface (CLI) it should print output to STDOUT (i.e. your terminal window when calling it manually).

In this case you should be able to re-direct all text output to a file when calling the script without a need to modify the script itself by

C:\...\matlab\bin> matlab -wait -minimize -nodesktop \
 -automation -r "run('C:\...\Source2.m');exit;" > FILEPATH

I hope this is what you want and the above soultion works for you (on Windows it seems to me?).

If you do not understand this solution you might want to read on "output re-direction", it can be very handy in many cases.

If I got your question wrong and you knew about all that, sorry for boring you with basic stuff.

However, when it comes to images, I do not know how to get those. They clearly won't be sent to STDOUT along with the text output. Lacking matlab I can only guess, but R's standart behaviour when invoked from the CLI is to create a file called plots.pdf in the working directory containing all plots. You could check for a similar file after running the scripts or read the matlab documentation.

mschilli
  • 1,884
  • 1
  • 26
  • 56
  • Hi, Thanks a lot for this help =) Indeed, I already know how to redirect outputs, but this gave me other ideas thought. =) – Charaf Jul 23 '13 at 13:07
  • @Charaf: If you manage to solve your problem by the means of those 'other ideas', do not forget to answer the question yourself or edit mine if it is close enough. – mschilli Jul 23 '13 at 13:50