Showing the figure window with octave - matlab

I am a very newbie to octave.
I want to show the figure I plot with octave, but no figure window shows up.
Here is what I want to plot, plot.m
x = load('ex2x.dat');
y = load('ex2y.dat');
figure % open a new figure window
plot(x, y, 'o');
ylabel('Height in meters')
xlabel('Age in years')
I run the script with the command line directly
octave plot.m
The dataset can be downloaded from http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=DeepLearning&doc=exercises/ex2/ex2.html
I have installed image package as the site suggested.
My OS is ubuntu 14.04, I am not sure if I miss something.
I appreciate any help.
Thank you!

I found the answer which can be found in Octave's FAQ.
http://wiki.octave.org/FAQ
If you are running an Octave script that includes a plotting command,
the script and Octave may terminate immediately. So the plot window
does show up, but immediately closes when Octave finishes execution.
Alternatively, if using fltk, the plot window needs a readline loop to
show up (the time when Octave is sitting around doing nothing waiting for interactive input).
A common solution is to put a pause command at the end of your script.
So I just to add pause in the end of my script and it shows the window.

Related

How can I run an octave command from the command line, and be able to have a window pop up? [duplicate]

I am trying to run a function in octave from the command line. The function is currently run like so:
octave --silent --persist --eval 'function(input arguments)'
function.m contains a plot command. When I invoke octave with the above command line parameters, the plot does show but octave enters into its interactive mode. My question is:
Is there any way to get octave to display the plot without entering the interactive mode when it is invoked from the command line?
Just use pause after your plotting functions
You can use:
waitfor(h)
at the end, which waits for you to close the figure.
AFAIK, the plot window is a child process of octave and therefor can only be displayed when octave is running. Even if you plot something from the "interactive" mode leave the plot open and close octave, the plot will also disappear.
What you could do is to plot to some output file like posted here:
f = figure
set(f, "visible", "off")
plot([1,2,3,4])
print("MyPNG.png", "-dpng")
You need to select a proper graphics toolkit:
available_graphics_toolkits
ans =
{
[1,1] = fltk
[1,2] = gnuplot
}
The default is fltk which cannot write to file without displaying the plot. However, if you select gnuplot it will be able to write to file without displaying it first. In your file start with:
graphics_toolkit gnuplot
The problem is that when you run from command line, when it ends, the plot windows disappear with it.
#! /usr/bin/octave -qf
f = figure;
set(f, "visible", "off")
t=0:0.001:5*pi;
plot(t, sin(5*t)), grid
print("MyPNG.png", "-dpng")
This saves output to MyPNG.png in the directory where it is run.
Then you might open it with a visualization program.
Another option is to add
pause
at the end of the program so it waits for user input to terminate, therefore to close the plot window.
Cheers :)
Also can try wait for key.
while (waitforbuttonpress ()==0)
pause(1)
end

How can i find which all MATLAB files have been executed during a simulation?

I am running a MATLAB simulation. It starts from a file top_file.m and it calls some other .m files and this may indeed calls other .m files like that.
Is there a way that I can know which all files were executed during the simulation, and if possible the order in which they were executed?
There is a built-in function matlab.codetools.requiredFilesAndProducts since R2014a which will return a list of the dependencies of any file without having to execute it.
>> files = matlab.codetools.requiredFilesAndProducts('test.m')
files =
'/path/to/test.m'
'/path/to/test.m/dependencies'
As pointed by Adriaan's comment Matlab's profiler tool does exactly what you want and much more!
You can run it from command line:
>> profile clear; profile on; %// clear history and start the tool
>> top_file; %// run your code
>> profile off; %// switch off the tool
>> profile viewer; %// launch GUI to view results
In the profiler's GUI you'll see the functions that were called, and their run time.
Try it! It is a very useful tool.

Prepare command in MATLAB

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.

Use Matlab PDE toolbox from command line

I would like to solve a PDE with Matlab PDE toolbox using only the command window of the system. I can create the problem and run the solver, but the PDE toolbox window pops up occaisionally and asks questions (e.g., "Do you want to save unsaved document?").
How can I avoid these popups or how can I use the PDE toolbox without opening its window?
I am using the following code. The window is pops up when I call the pdeinit function on the first line.
[pde_fig,ax]=pdeinit;
set(ax,'XLim',[-0.1 0.2]);
set(ax,'YLim',[-0.1 0.2]);
set(ax,'XTickMode','auto');
set(ax,'YTickMode','auto');
% Geometry description:
pderect([0 0.05 0.05 0],'R1');
pderect([0 0.1 0 0.1],'R2');
set(findobj(get(pde_fig,'Children'),'Tag','PDEEval'),'String','R2-R1');
...
The help for pdeinit is short: "Start PDETOOL from scripts." pdetool, like most *tool M-files from The MathWorks, is a GUI and the help/documentaion for it indicates as much.
I'm confused because, not only does pdeinit open a figure window, but you're using it to return handles to the figure and axis of that figure. Your code then proceeds to manipulate those handles. You can't get those handles without first creating and opening a figure. Is the issue that you just want a regular figure window instead? If so, then you can replace [pde_fig,ax]=pdeinit; with:
pde_fig = figure;
ax = gca;
You can look at the code for pdeinit: type edit pdeinit in your command window. You'll see that all it does is open pdetool (unless it's already open) and return handles to the resultant figure and axis.
Additionally, pderect will open pdetool on its own. You're using a bunch of functions that are all tied to the PDE app. Many of the tutorials and examples on The MathWorks's site use this. You might check out this article on how to solve PDEs programmatically. The examples might also be helpful.

How to make output of Matlab m file to stay for a while

I have matlab m file which plots 3 graphs using subplot. Now i also have a UNIX script which invokes this script and passes the function name as parameter.
I have two problems:
I am getting the warning Type-ahead buffer Overflow
The plot remains only for a few seconds before disappearing. How can I keep plot active til l user clicks on cross button?
thanks!
It's hard to answer your first question without some code.
You can use the pause command to wait for the user to press any key.