Related
I know it's possible to forward any output from a remote machine to a local one by using the X11 forwarding remote tunnelling, so that when you run a MATLAB command it will display all the graphical outputs back to the machine you've connected from.
My question is:
Is there any MATLAB command to just output the figures (e.g., plot,surf,etc.) without displaying any other graphical object (i.e., the main interface)?
In practice, I would like to interact with MATLAB by using the command line (as shown below) and forward back only the figures.
MATLAB cannot display figures without its own figure-GUI, so the answer to your question would be no.
However: there is a workaround: create an invisible figure using f=figure('visible', 'off'), then plot your data, and finally use saveas(f,filename,fileextention). Don't forget to close(f) your figure after saving, to free the RAM. You'll now have a figure in your file directory, which you can display using your favourite visualising tool, which might even be possible through a call to system, although I have never tested that.
I'd like to create a PDF out of my matlab m-function. The PDF should contain some text information which I want to style a bit and one image (which is previously generated as figure). Is there any way? The only thing I found is publish to publish source-code. The only alternative I could think off was to programm the texts into the figure window and than export the whole figure to pdf. Perhaps there's a better way to do it?
Thanks!
I would recommend generating your figure with Matlab, outputting it to eps, then converting the figure to pdf using epstopdf. Then embed your figure in a latex document and generate a pdf with pdflatex.
I know this sounds like an incredibly roundabout way of doing it, but
This is the way I've come to do it after years of experience and it always gives me the best results
Every one of my colleagues does it this way for the same reason
The results will be completely reproducible
You're using Matlab to do what it's good at (making scientific figures) and latex to do what it's good at (formatting documents)
The Matlab code to make the eps of the figure would be like this (supposing your figure is figure 1):
print -depsc2 -f1 -loose my_fig.eps
You could pretty easily write a latex template that uses my_fig.pdf and then run everything from your Matlab using bangouts:
!epstopdf my_fig
!pdflatex mydoc.tex
If you're on Windows, you can connect to Word using 'actxserver', insert and style any text you like into a blank document, copy and paste MATLAB figures into the document, and then save it to PDF. You can do all of that from within MATLAB. The first time you do this it's a pain, as you need to learn quite a bit about the Word Object Model; but once you've done it a couple of times it's very simple and quick, and you can achieve very professional results. You can combine this with using the 'export_fig' that others have mentioned.
Although many of you will have a decent idea of what I'm aiming at, just from reading the title -- allow me a simple introduction still.
I have a Fortran program - it consists of a program, some internal subroutines, 7 modules with its own procedures, and ... uhmm, that's it.
Without going into much detail, for I don't think it's necessary at this point, what would be the easiest way to use MATLAB's plotting features (mainly plot(x,y) with some customizations) as an interactive part of my program ? For now I'm using some of my own custom plotting routines (based on HPGL and Calcomp's routines), but just as part of an exercise on my part, I'd like to see where this could go and how would it work (is it even possible what I'm suggesting?). Also, how much effort would it take on my part ?
I know this subject has been rather extensively described in many "tutorials" on the net, but for some reason I have trouble finding the really simple yet illustrative introductory ones. So if anyone can post an example or two, simple ones, I'd be really grateful. Or just take me by the hand and guide me through one working example.
platform: IVF 11.something :) on Win XP SP2, Matlab 2008b
The easiest way would be to have your Fortran program write to file, and have your Matlab program read those files for the information you want to plot. I do most of my number-crunching on Linux, so I'm not entirely sure how Windows handles one process writing a file and another reading it at the same time.
That's a bit of a kludge though, so you might want to think about using Matlab to call the Fortran program (or parts of it) and get data directly for plotting. In this case you'll want to investigate Creating Fortran MEX Files in the Matlab documentation. This is relatively straightforward to do and would serve your needs if you were happy to use Matlab to drive the process and Fortran to act as a compute service. I'd look in the examples distributed with Matlab for simple Fortran MEX files.
Finally, you could call Matlab from your Fortran program, search the documentation for Calling the Matlab Engine. It's a little more difficult for me to see how this might fit your needs, and it's not something I'm terribly familiar with.
If you post again with more detail I may be able to provide more specific tips, but you should probably start rolling your sleeves up and diving in to MEX files.
Continuing the discussion of DISLIN as a solution, with an answer that won't fit into a comment...
#M. S. B. - hello. I apologize for writing in your answer, but these comments are much too short, and answering a question in the form of an answer with an answer is ... anyway ...
There is the Quick Plot feature of DISLIN -- routine QPLOT needs only three arguments to plot a curve: X array, Y array and number N. See Chapter 16 of the manual. Plus only several additional calls to select output device and label the axes. I haven't used this, so I don't know how good the auto-scaling is.
Yes, I know of Quickplot, and it's related routines, but it is too fixed for my needs (cannot change anything), and yes, it's autoscaling is somewhat quircky. Also, too big margins inside the graf.
Or if you want to use the power of GRAF to setup your graph box, there is subroutine GAXPAR to automatically generate recommended values. -2 as the first argument to LABDIG automatically determines the number of digits in tick-mark labels.
Have you tried the routines?
Sorry, I cannot find the GAXPAR routine you're reffering to in dislin's index. Are you sure it is called exactly like that ?
Reply by M.S.B.: Yes, I am sure about the spelling of GAXPAR. It is the last routine in Chapter 4 of the DISLIN 9.5 PDF manual. Perhaps it is a new routine? Also there is another path to automatic scaling: SETSCL -- see Chapter 6.
So far, what I've been doing (apart from some "duck tape" solutions) is
use dislin; implicit none
real, dimension(5) :: &
x = [.5, 2., 3., 4., 5.], &
y = [10., 22., 34., 43., 15.]
real :: xa, xe, xor, xstp, &
ya, ye, yor, ystp
call setpag('da4p'); call metafl('xwin');
call disini(); call winkey('return');
call setscl(x,size(x),'x');
call setscl(y,size(y),'y')
call axslen(1680,2376) !(8/10)*2100 and 2970, respectively
call setgrf('name','name','line','line')
call incmrk(1); call hsymbl(3);
call graf(xa, xe, xor, xstp, ya, ye, yor, ystp); call curve(x,y,size(x))
call disfin()
end
which will put the extreme values right on the axis. Do you know perhaps how could I go to have one "major tick margin" on the outside, as to put some area between the curve and the axis (while still keeping setscl's effects) ?
Even if you don't like the built-in auto-scaling, if you are already using DISLIN, rolling your own auto-scaling will be easier than calling Fortran from MATLAB. You can use the Fortran intrinsic functions minval and maxval to find the smallest and largest values in the data, than write a subroutine to round outwards to "nice" round values. Similarly, a subroutine to decide on the tick-mark spacing.
This is actually not so easy to accomplish (and ideas to prove me wrong will be gladly appreciated). Or should I say, it is easy if you know the rough range in which your values will lie. But if you don't, and you don't know
whether your values will lie in the range of 13-34 or in the 1330-3440, then ...
... if I'm on the wrong track completely here, please, explain if you ment something different. My english is somewhat lacking, so I can only hope the above is understandable.
Inside a subroutine to determine round graph start/end values, you could scale the actual min/max values to always be between 1 and 10, then have a table to pick nice round values, then unscale back to the correct range.
--
Dump Matlab because its proprietary, expensive, bloated/slow and codes are not easy to parallelize.
What you should do is use something on the lines of DISLIN, PLplot, GINO, gnuplotfortran etc.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm getting some new students soon, who will be writing MATLAB code. They're new to MATLAB, but they have experience coding in Java and C++.
I'm going to have them go through the Getting Started section of the MATLAB help. In addition, I want to give a small tutorial with the goal to prevent them from making some of the most common mistakes people make when switching to MATLAB (e.g. "MATLAB starts counting at 1"), and show them some features that they may not be aware of when coming from other languages (e.g. "you can subtract a scalar directly from an array, and for vectors, there's bsxfun").
What are the most important things I should tell them?
I agree with previous answers, but I'd say indexing is the first and the most important and complex concept in studying MATLAB. I saw many C programmers starting with MATLAB just write loops, a lot of loops, something ridiculous like
for i=1:10
a(i)=i;
end
instead of simple a=1:10;.
So I'd suggest them to read about matrix programming concepts:
How to create simple vectors and matrices
Which variables can be used for indexing
How to create and apply indexes
Logical operations and functions, logical and numeric indexes (find function)
Indexing right and left side of expression
Difference between indexing numerical matrices and cell arrays
How to use indexes as output from different functions, like sort, unique, ismember, etc.
You cannot apply indexes to intermediate results
As for productivity, I would add that knowing how to use editor's cell mode is very useful.
Enough snippy comments, here's something of an answer too:
The Matlab desktop: what all the windows are for, dragging code from the history back into the command window, the variable inspector, etc.
Plotting: not just the plot command, but how to use the plot GUI tools, and how to create an M-file from a graphic.
M-files for scripts and functions, and the key differences between them.
M-Lint, the profiler.
Use Matlab as a vehicle for teaching the perils and pitfalls of floating-point arithmetic.
Getting help: at the command line, on the web, documentation, the file exchange, ...
Set path and the current working directory.
Importing data from files, exporting data to files, loading and saving.
That should be enough to keep them busy for an hour or so.
To clarify, I propose these topics to help you teach your students to avoid common Matlab errors including;
Unproductive use of the tool, retyping commands which can easily be recalled from the history, using C (or Java) style file reading commands instead of uuimport, slowly typing scripts to draw graphics when Matlab can do it for you, wondering what all the little orange lines in the editor right margin mean and the squiggly underlines, trying to figure things out for themselves when the help facilities could tell them, tons of other stuff that many much more experience Matlab users have taken ages to learn.
Floating point arithmetic is not real.
and probably a lot of other stuff too.
For those coming from C-family languages, the element-wise operators are new. It took me a couple of months to discover the ./ and .* operators. Before that, I used to write for loops for element-wise operations. So perhaps that's something that should be pointed out.
With respect to unexpected or non-intuitive MATLAB features that may cause them confusion, there are some good pointers in this question:
Corner Cases, Unexpected and Unusual MATLAB
With respect to cool time-saving/efficiency tricks, this other question has some nice examples:
What are your favourite MATLAB/Octave programming tricks?
And for a few potentially more advanced topics, you can refer to the answers to this question:
MATLAB interview questions?
Now for my $0.02. Based on the sorts of questions I've seen asked most frequently on SO, I'd say you will want to make sure they have a good understanding of the following concepts:
Reading and writing data files of different formats, such as using CSVREAD, DLMREAD, TEXTREAD, FREAD, FSCANF, LOAD, and all their write equivalents.
How to deal effectively with cell arrays.
The different image formats, how these are represented, and how to modify them (which will involve a discussion of various data types and how to deal with multi-dimensional arrays).
How to use handle graphics to control the appearance of various graphics objects.
And here are some neat features that are already implemented in MATLAB that may save them some time and effort:
Functions for performing various array operations, like KRON, DIAG, and TRIU.
Functions to create specialized matrices, like HANKEL and TOEPLITZ.
Predefined dialog boxes, like UIGETFILE and INPUTDLG.
MATLAB is conceptually in some ways very different from other languages you mentioned:
cells are used were Java uses upcasting
global and persistent variables are static in Java
gui handles being just numbers of type double
nested functions are closures, neither Java nor C/C++ has such feature
seldom used private and #TYPE folders for visibility scoping
array handling tricks
very easy interoperability with Java/COM/.Net using MATLAB syntax
variadic function arguments, handling of function arguments with varargin / varargout
memory management
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How have you tweaked the MATLAB environment to better suit your needs? One tweak per answer.
I run "format compact" to remove all those frustrating blank lines spacing out my output. Oh so simple, but makes it so much nicer to me.
I use a function idetitle() that can change the window title of the Matlab GUI itself. Useful in a development environment where I'm running several Matlab processes, possible on different branches of source code or model runs. Sometimes I'll put the PID in the window title to make it easy to find in Process Explorer for monitoring resource usage.
function idetitle(Title)
%IDETITLE Set Window title of the Matlab IDE
%
% Examples:
% idetitle('Matlab - Foo model')
% idetitle(sprintf('Matlab - some big model - #%d', feature('getpid')))
win = appwin();
if ~isempty(win)
win.setTitle(Title);
end
function out = appwin()
%APPWIN Get main application window
wins = java.awt.Window.getOwnerlessWindows();
for i = 1:numel(wins)
if isa(wins(i), 'com.mathworks.mde.desk.MLMainFrame')
out = wins(i);
return
end
end
out = [];
I changed the default font in the MATLAB editor to 10 point ProFont (which can be obtained here) so I could write code for long periods of time without giving myself a headache from straining my eyes.
I run Matlab with the options -nodesktop -nojvm. That way it just sits in a terminal out of the way, and I can use my favourite text editor to my heart's content.
You do miss out of some killer features this way though.
I set the number of lines in the command window scroll buffer to the maximum (25,000). This doesn't seem to noticeably affect performance and allows me to display a large amount of data/results.
I use a startup.m file (sits in the local MATLAB path) to make sure that I have the settings I want whenever I start up MATLAB. This includes such things as formatting the REPL and plot parameters.
I set the Command Window output numeric format to long g.
I implemented analogues of xlim and ylim: xlim_global([xmin xmax]) and ylim_global([ymin ymax]), which sets the axes' limits the same for every subplot in the figure.
I invert colors to have a black backgroud, easier on the eyes.
(Alt+Shift+PrintScreen on Windows, you can configure away the huge icons)
I keep a diary for each session (possibly multiple diary files per day) to recall all commands executed. This is controlled by a startup.m file that checks for previous diary files from that day.
I wrote a small function called fig.m to call up figure windows with names rather than numbers and display the name in the status bar.
Funnily enough, there are two or three identically named files that do exactly the same thing on the file exchange.
I have functions to 1) save the current figure locations and sizes on the screen, and 2) and one to load such configuration. It's very useful e.g. when monitoring data-heavy simulations.
I set shortcuts for
open current directory
up 1 folder
an action to do 'close all; clear all; clc;'
Ref:
http://www.mathworks.com/matlabcentral/fileexchange/19097-custom-panzoom-icons
send the outputs to your email esp when the running is long
http://www.mathworks.com/matlabcentral/fileexchange/29183-sending-reports-and-timestamped-file-by-emailing
create a result collector for archiving and sending
http://www.mathworks.com/matlabcentral/fileexchange/29255-track-collect-and-tar-inputs-and-outputs
a patch to line up the file within a directory in proper order
http://www.mathworks.com/matlabcentral/fileexchange/29033-file-ordering-patch-utility-for-matlab