Print command window output on command prompt of windows (cmd)

Hey everyone,
I hope you all are in good health.
I have a question about how to automatically write everything that displays on command windows simultaneously on the command prompt of windows (cmd).
here is what I have found for storing the command window of Matlab to a file and then publishing it as an HTML file to the browser,
myFile = 'commandWindowText.txt';
if exist(myFile, 'file')==2
delete(myFile);
end
diary(myFile)
diary on
% ---- || what i want to display in the command window
syms x
eq = 'x^4 + 2*x + 1';
s = solve(x^4 + 2*x + 1, x,'MaxDegree',3);
disp(['the equation is: ' eq])
disp(['and the result is: ' ])
pretty(s)
diary off
% ---- || open the file to get the content as a string
fid = fopen(myFile,'r');
f=fread(fid,'*char')';
fclose(fid);
% ---- || adapt the text file to html
f=strrep(f,'\','\\'); % thanks to mahoromax's comment (accomodates windows file paths)
f=strrep(f,newline,'<br>');
f=strrep(f,' ','&nbsp');
f=['<p style = "font-family:monospace" >',newline,f,newline,'</p>'];
% ---- || write the file and view it on the broweser
fid=fopen('diary_file.html','w');
fprintf(fid,f);
fclose(fid);
winopen('diary_file.html') % windows only?
in addition to the HTML version i need to send this information to the cmd of windows, but i dont know how
1- open command prompt
2 - send text to command prompt.
any help appreciated.

6 Comments

What is your goal in sending that text? Do you just want to display it (with echo)? Or do you want to run some command?
Matlab is generally not the best tool to send keystrokes to external programs. If that is your goal you might consider pasting to the clipboard (see the clipboard function).
yes, I need to display some text in an external shell
Do you need the command prompt to be interactive afterward? Because I'm not aware of a way to do that. And should it be a specific window, or can it be any cmd window?
Note that the term shell generally means a Unix prompt, which you can get on windows with WSL.
dear Rik,
the best situation is an interactive way between the command prompt and the code in the Matlab, for instance by creating a mutual text file or database and etc.
but for now, I'm satisfied with a simple display from Matlab to the command prompt.
actually, I want to create a stand-alone app with an app designer and I need to show some progress and results on the command prompt.
I think about our first solution, I mean clipboard function but I couldn't implement it.
if you could help me I really appreciate you.
"I want to create a stand-alone app with an app designer and I need to show some progress..."
App Designer doesn't built a console; use something like a progress bar or whatever controls in the app widget collection instead.
Don't try to pound a square peg into a round hole; the way you're trying to go is not going to be satisfactory even if you can manage to cobble something together.
Dear @dpb
thank you for your explanation,
I understood what you try to transfer to me.
just forget about the app designer.
so you told me there is no logical way to paste/display something in the command prompt from Matlab Neither with Matlab routine code nor with existing Matlab-java codes.
I'm disappointed :/

Sign in to comment.

 Accepted Answer

If you are using Windows then use .NET controls System.Diagnostics.Process to create a link to either PowerShell or CMD.exe. Anything that you write to the end point will then be sent to the process to be executed (or interpreted as input).
However, I do not understand how executing the matlab output as shell commands will be beneficial in any way. But that is what pasting the output to the command prompt means, that you want the output to be executed as commands.

8 Comments

"actually, I want to create a stand-alone app with an app designer"
syms x
You appear to be using the Symbolic Toolbox. Nothing in the Symbolic Toolbox can be compiled with MATLAB Compiler , or MATLAB Coder, so you cannot use syms in a standalone app.
thank you for your guidance.
actually, I need to print the output of my work which achieving from Matlab to cmd.exe
yes, I'm using windows, and I try to understand how to create a link between cmd.exe and Matlab with .Net
and also I try to understand the properties of .Net by using
properties(System.Diagnostics.Process)
but it doesn't help me to understand how to build a connection.
Any ideas would be much appreciated.
forget about app designer.
in an easy way explanation, I have some string/text in Matlab and I need to print them in cmd.exe automatically by Matlab.
https://www.mathworks.com/matlabcentral/answers/586706-how-to-redirect-standard-input-and-standard-output-using-net-system-diagnostics-process#answer_487475
You have said that you are running some MATLAB code and you need to display some output and progess. You also say that it needs to go to CMD.exe .
I would like to ask whether the output really needs to go to CMD.exe, or if instead you just need the text output to go to a text window (perhaps a scrollable text window)? Because outputting to a (scrollable?) text window is a completely different need than having it go to CMD.exe.
You say to forget about App Designer, and to forget about standalone, but those are both important details. If you have a live matlab session that is not App Designer then there are options that are not otherwise available, and compiling reduces your options more.
the whole story is I have two codes one in Matlab and the other in GoLang.
but the log and the results are mutual.
one approach is storing both results on separate text files and then at some certain times append both logs to one file in order to aggregate the results.
and finally, build a front that shows the aggregated results for end-user.
another way is in a real-time manner, all of the outputs print in a common shell/ windows or etc.
I try to reach the second approach. but until now all of the efforts fail.
MATLAB can run the GoLANG process, using System.Diagnostics.Process . Or GoLANG can run MATLAB and read its standard output and standard error. Or something else could invoke each of them and take care of merging the outputs.
Note that in MATLAB if you fopen() a file with 'a' access, then every fprintf() or fwrite() to the file will result in a fseek() to end of file, even if a different program is writing to the same file . So if you were careful to do things like
NOW = char(datetime('now', 'Format', 'yyyyMMdd HH:mm:ss.SSS'));
fprintf(fid, 'AN7: %s: error coefficient is %.5g\n', NOW(), slice_err);
which writes an entire (time-stamped) line, then the operating system would guarantee that it will appear as a line by itself, not scrambled up with output from the GoLang program.
thank you for your consideration.
i will try to call Matlab scrip from my GoLang. i think it is the best soulution for me.
kind regards,
Abolfazl

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!