1 I run matlab without GUI, sometimes I would like to see commandhisoty by using commandhistory function, but I found that if I run matlab without GUI, the program cannot save any command history like the GUI matlab, are there some methods to solve this problem?
2 I tyied to use diary, but it seems that it can only save all commands and outputs. In addition, it seems that I should type diary every time when I open matlab, the default option is diary off. How to make it more convenient to use
Related
How do I display the current value of a variable in the matlab command window? In matlab I usually use disp(var) and it will output the variable value into the command window. However in the MATLAB function block if I were to enter disp('hello') it doesn't show any output in the command window. I am using matlab 2014b.
You idea for displaying output value in the command window is not a good approach to solve your issue. I would suggest you look at scope block if you'd like to see the value during simulation. Or use To Wrokspace block if you'd like to output the value into main workspace.
For debugging your code and design, read and use Simulink Debugger should be helpful.
I am trying to write a batch script which can call and run a MATLAB script in the following manner:
matlab -r plotFunction(a,b); quit %here, a=1:10 and b=1:10
matlab -r plotFunction(a,b); quit %in 2nd instance a=11:20, b=11:20
matlab -r plotFunction(a,b); quit %in 3rd instance a=21:30, b=21:30
and so on.
That is, each time a new instance of MATLAB opens up, calls the function plotFunction which performs plotting a 100 times and then the program (MATLAB) quits. Subsequent to this, another instance of the program opens, performs plotting a 100 times again (corresponding to a=11:20 and b=11:20) and quits again. And so forth. How to put this in a loop?
The batch_job toolbox does this for you.
You can pass variables defined in the Windows command prompt to MATLAB line this:
set AMIN='1'
set BMIN='1'
set AMAX='10'
set BMAX='10'
matlab -r "disp(str2double(%AMIN%):str2double(%AMAX%)),disp(str2double(%BMIN%):str2double(%BMAX%)); input('press a key to quit'); quit"
Edit:
This can be improved like this,
set AMIN=1
set BMIN=1
set AMAX=10
set BMAX=10
set MATPROG=^
arange=(%AMIN%:%AMAX%),^
brange=(%BMIN%:%BMAX%),^
[x,y]=meshgrid(arange,brange),^
aplusb=x+y,^
plot3(x,y,aplusb),^
input('press a key to quit'),^
quit
matlab -r "%MATPROG%"
Note that ^ is the batch file line continuation character.
This change will make it easier to convert to loops in the batch file, although I don't understand why you don't create the loops in a MATLAB function and call that instead to keep the batch files as simple as possible.
Is there a way in MATLAB to prepare a command programmatically (ie. writing a command directly to the command prompt) so the user can execute it by pressing an enter?
I want to implement my own "Did you mean:" functionality, that is built into MATLAB already.
It can be done using Java from Matlab to programmatically generate key events, along the lines of this answer.
Let's say the command you want to "prepare" is dir. Then
commandwindow; %// make Matlab command window have focus
robot = java.awt.Robot; %/ Java Robot class
robot.keyPress (java.awt.event.KeyEvent.VK_D); %// key press event
robot.keyRelease (java.awt.event.KeyEvent.VK_D); %// key release event
robot.keyPress (java.awt.event.KeyEvent.VK_I);
robot.keyRelease (java.awt.event.KeyEvent.VK_I);
robot.keyPress (java.awt.event.KeyEvent.VK_R);
robot.keyRelease (java.awt.event.KeyEvent.VK_R);
will type dir onto the command window, exactly as if the user had written it. Then pressing Enter will run the command.
The short answer is no. This can't be done as you want it to. What you are trying to do is to write text to MATLAB's stdin and let it remain there unprocessed. Essentially a modified form of piping.
The closest option available in MATLAB is Evaluate Selection where when you select text you can cause MATLAB to execute it in the command prompt. MATLAB puts this text exactly where you want it but it also immediately executes it. There does not seem to be a way to halt this or emulate its functionality programmatically.
Writing to stdin in MATLAB is not allowed as you can see from
>> fprintf(0, 'help conv')
Error using fprintf
Operation is not implemented for requested file identifier.
where 0 denotes stdin, 1 denotes stdout and 2 denotes stderr. Another naive attempt would be to use fopen()
>> fid = fopen(0, 'w');
Error using fopen
Invalid filename.
but alas this fails too. However, we can see from the first attempt that what you desire
is not implemented
The only option to get exactly what you want is that possibly with some MATLAB hackery the ability is there but I'm not aware of it or anybody who has even attempted it.
EDIT: Luis Mendo has provided the MATLAB hackery solution I was talking about.
The closest you could get to what you want is to use hyperlinks to run MATLAB commands like
>> disp('Did you mean: conv()')
Did you mean: conv()
ans =
12
where conv() is a hyperlink and clicking on it will execute conv(a,b) where a = 3; and b = 4; in this example.
I use matlab without GUI, I wonder how I can run part of the matlab codes in terminal.
I can run part of the codes in the default matlab editor.
reference:
matlab-emacs
http://www.mathworks.com/help/matlab/matlab_prog/run-sections-of-programs.html
I have a script that calls another program (EES - Engineering Equation Solver) and performs some calculations there and returns the results. The problem I am encountering is that everytime I call the EES from Matlab, it starts the EES but does not solve the EES equations. Instead, I have to then manually press the "solve" button inside the EES, close the EES and after that the results are returned to Matlab.
Is it possible to use a command inside Matlab to solve the EES equations automatically?
The current solution I have is the following, but it only starts the EES and does not solve it automatically:
system ('c:\ees32\ees.exe c:\EES32\testing\solverfile.ees /solve'); %call EES and solve
Thanks!
On a second thought, this is something that is dependent on the EES program and the commands that are possible to use there. So this is probably not the best place for asking this questions.