MATLAB.jl
MATLAB.jl copied to clipboard
Saving figure using export_fig using gcf
I'm attempting to use some old plotting code, and I can't seem to get export_fig to save. Here's a minimal example showing the behavior.
using MATLAB
y = linspace(2., 3., 500)
mat"plot($(y))"
mat"export_fig(gcf,'test.png')"
I can't find test.png anywhere on the system, and neither Julia nor Matlab gives an error.
where does it save if you run the file from matlab alone?
check the directory
julia> mat"pwd"
"C:\\Program Files\\MATLAB\\R2016b"
One option is to start the MSession in the julia start directory
i.e. inserting ccall(engfunc(:engEvalString), Cint, (Ptr{Void}, Ptr{UInt8}), ep, #directory of julia start)
good catch, looks like locate wasn't updating as fast as I thought it would (also, thanks for the fast turn around!). The minimal example does what it's supposed to. I'm running the original code to see if I can get it to reproduce the error (the first time gave an error message along the lines of "failed to get ans from a MATLAB session", but further calls were silent).
Yep still getting errors on the real code. The Julia error is MATLAB.MEngineError "failed to get the variable ans from a MATLAB session". while the MATLAB error is {Error using save Variable 'ans' of class 'matlab.graphics.primitive.Surface' cannot be saved to STDIO. }
Strange, I can't reproduce. Are you sure you can't reproduce those errors from matlab alone?
the code above works fine- I have significantly messier code that is producing the above errors when called through Julia. The command is
mat"plotImportantScat($(β), $(Sname), $(CLASSNAMES), 'randoCal')"
where plotImportantScat is the function which calls export_fig, and \beta, Sname, and CLASSNAMES are all Julia variables. I ran this function (there's ~170 lines, which is why I haven't posted it) on different data in MATLAB earlier today, and it gave no errors.
Running it again, I've found that if I don't return the handles for the plot, it doesn't return the error and saves. Perhaps there's something with the way MATLAB interacts with csh that causes this error?
So I can't seem to reproduce on my linux machine
Also on linux it seems that the path will open by default in the current directory (not the case for windows)
julia> using MATLAB
julia> y = linspace(2., 3., 500)
500-element LinSpace{Float64}:
2.0,2.002,2.00401,2.00601,2.00802,2.01002,2.01202,2.01403,2.01603,…,2.98597,2.98798,2.98998,2.99198,2.99399,2.99599,2.998,3.0
julia> mat"plot($(y))"
>> >> >> Warning: MATLAB has disabled some advanced graphics rendering features by
switching to software OpenGL. For more information, click <a
href="matlab:opengl('problems')">here</a>.
julia> mat"export_fig(gcf,'test.png')"
>> >> >> Warning: MATLAB cannot use OpenGL for printing when started with the
'-nodisplay' option.
> In inputcheck (line 160)
In print (line 41)
In print2array (line 183)
In export_fig (line 518)
julia> ^C
which version of matlab and which julia version and are you on master MATLAB.jl ?
R2015a, and I believe I'm on master. I updated earlier today.
Apologies for mis-identifying the source of the error earlier. Here's a block of code that should reproduce the error. define the following function in Matlab (I did it using a file in the current Matlab directory)
function handel = test(bar)
handel = plot(bar)
end
then call it in julia using
using MATLAB
x = linspace(-10., 10., 500)
mat"test($(x))"
this returns the error MATLAB.MEngineError "failed to get the variable ans from a MATLAB session" in get_mvariable at MATLAB/src/engine.jl:136 in get_variable at MATLAB/src/engine.jl:143 {Error using save Variable 'ans' of class 'matlab.graphics.chart.primitive.Line' cannot be saved to STDIO. }
cc @simonster
Presumably you are on a linux machine? I'm not familiar with csh, but maybe it's an older version or something? The one I tried is ii csh 20110502-2.1ubunt amd64 Shell with C-like syntax
@dsweber2 what happens when you try julia> eval_string("test($x)")
For me
julia> eval_string("test($x)")
handel =
Line with properties:
Color: [0 0.4470 0.7410]
LineStyle: '-'
LineWidth: 0.8000
Marker: 'none'
MarkerSize: 6
MarkerFaceColor: 'none'
XData: [1�500 double]
YData: [1�500 double]
ZData: [1�0 double]
Use GET to show all properties
I'm not at all familiar with csh, but was going off of the readme's description of what the interface with matlab is. I am on Ubuntu 16.04.1 LTS, and julia v0.5.0
interesting, when I ran eval_string("test($(x))") I got slightly more (but effectively the same):
>>
handel =
Line with properties:
Color: [0 0.4470 0.7410]
LineStyle: '-'
LineWidth: 0.5000
Marker: 'none'
MarkerSize: 6
MarkerFaceColor: 'none'
XData: [1x500 double]
YData: [1x500 double]
ZData: [1x0 double]
Use GET to show all properties
ans =
Line with properties:
Color: [0 0.4470 0.7410]
LineStyle: '-'
LineWidth: 0.5000
Marker: 'none'
MarkerSize: 6
MarkerFaceColor: 'none'
XData: [1x500 double]
YData: [1x500 double]
ZData: [1x0 double]
Use GET to show all properties
comparing this to when I run mat"test($(x))" at a terminal:
>> >> >>
handel =
Line with properties:
Color: [0 0.4470 0.7410]
LineStyle: '-'
LineWidth: 0.5000
Marker: 'none'
MarkerSize: 6
MarkerFaceColor: 'none'
XData: [1x500 double]
YData: [1x500 double]
ZData: [1x0 double]
Use GET to show all properties
Error using save
Variable 'ans' of class 'matlab.graphics.chart.primitive.Line' cannot be saved
to STDIO.
ERROR: MATLAB.MEngineError("Failed to get the variable ans from a MATLAB session.")
in get_mvariable(::MATLAB.MSession, ::Symbol) at /home/david/.julia/v0.5/MATLAB/src/engine.jl:136
in get_variable(::Symbol) at /home/david/.julia/v0.5/MATLAB/src/engine.jl:143
The mat string literal tries to return the result of the MATLAB expression. The problem is that MATLAB apparently does not always support getting all types of objects via the API, so in this case it is throwing an error. Not sure what the correct behavior is here; we could warn or just return nothing.
@simonster currently it spits an ERROR: ArgumentError: Unsupported kind of variable.
Perhaps a warning is more appropriate and to return nothing?
I see. For my usage, at least, a warning that recommends using eval_string instead would be sufficient, since I wasn't planning on trying to edit the plothandles in Julia anyways.
julia> mat"pwd" "C:\Program Files\MATLAB\R2016b"
I think it is better that automatically you'll move to the user path by (MATLAB syntax) - cd(userpath()).
I actually think it will be the most useful to have a dedicated package to use MATLAB's plotting library. Just like PyPlot.jl. MATLAB interactive figures are something we can't (At least I couldn't find how) reproduce in Julia (As Matplotlib still doesn't have this smooth operation).