I need to write code that should run equally well in Octave and on MATLAB. Problem is that it needs to do some GUI stuff, which MATLAB and Octave handle completely differently.
Is there a way I can detect if I'm running MATLAB or Octave, in order to call the right function?
You could use the following test to differentiate Octave from MATLAB:
isOctave = exist('OCTAVE_VERSION', 'builtin') ~= 0;
There is also a hint in the wiki on the official octave.org website.
They propose the following:
Edit: Not all versions of Matlab support '#' for comments so I changed the example to use '%' instead. It works in Matlab R2018 (Linux) and Octave 4.2.2
function foo
%% fancy code that works in both
if (is_octave)
%% use octave super_powers
else
%% do it matlab way
end
%% fancy code that works in both
end
%% subfunction that checks if we are in octave
function r = is_octave ()
persistent x;
if (isempty (x))
x = exist ('OCTAVE_VERSION', 'builtin');
end
r = x;
end
I would use, for example, the ver command, which yields:
in MATLAB:
MATLAB Version 7.7.0.471 (R2008b)
Operating System: Linux 2.6.31-20-generic #57-Ubuntu SMP Mon Feb 8 09:05:19 UTC 2010 i686
Java VM Version: Java 1.6.0_04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
in Octave:
GNU Octave Version 3.0.5
GNU Octave License: GNU General Public License
Operating System: Linux 2.6.31-20-generic #57-Ubuntu SMP Mon Feb 8 09:05:19 UTC 2010 i686
Another possibility is to use the license function.
In Matlab:
>> exist __octave_config_info__
ans =
0
In Octave:
octave:3> exist __octave_config_info__
ans = 5
Related
I need to write code that should run equally well in Octave and on MATLAB. Problem is that it needs to do some GUI stuff, which MATLAB and Octave handle completely differently.
Is there a way I can detect if I'm running MATLAB or Octave, in order to call the right function?
You could use the following test to differentiate Octave from MATLAB:
isOctave = exist('OCTAVE_VERSION', 'builtin') ~= 0;
There is also a hint in the wiki on the official octave.org website.
They propose the following:
Edit: Not all versions of Matlab support '#' for comments so I changed the example to use '%' instead. It works in Matlab R2018 (Linux) and Octave 4.2.2
function foo
%% fancy code that works in both
if (is_octave)
%% use octave super_powers
else
%% do it matlab way
end
%% fancy code that works in both
end
%% subfunction that checks if we are in octave
function r = is_octave ()
persistent x;
if (isempty (x))
x = exist ('OCTAVE_VERSION', 'builtin');
end
r = x;
end
I would use, for example, the ver command, which yields:
in MATLAB:
MATLAB Version 7.7.0.471 (R2008b)
Operating System: Linux 2.6.31-20-generic #57-Ubuntu SMP Mon Feb 8 09:05:19 UTC 2010 i686
Java VM Version: Java 1.6.0_04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
in Octave:
GNU Octave Version 3.0.5
GNU Octave License: GNU General Public License
Operating System: Linux 2.6.31-20-generic #57-Ubuntu SMP Mon Feb 8 09:05:19 UTC 2010 i686
Another possibility is to use the license function.
In Matlab:
>> exist __octave_config_info__
ans =
0
In Octave:
octave:3> exist __octave_config_info__
ans = 5
I want install cvx on MATLAB 2015b. I followed the steps given on Matlab cvx error with cvx_begin.
However, I get following error
enter >> cd('C:\cvx')
cvx_setup
CVX: Software for Disciplined Convex Programming (c)2014 CVX Research
Version 2.1, Build 1110 (66e9a9c) Wed Jun 10 21:43:38 2015
Installation info:
Path: C:\cvx
MATLAB version: 8.6 (R2015b)
OS: Windows 7 x86 version 6.1
Java version: 1.7.0_60
Verfying CVX directory contents:
No missing files.
Preferences: none found; defaults loaded.
Setting CVX paths...done.
Saving updated path...
UNEXPECTED ERROR: ------------------------------------------------
Error using cd
Cannot CD to C:\Users\NIT2\AppData\Local\Temp (Name is nonexistent
or not a directory).
Error in tempdir (line 30)
curr_dir = cd(tmp_dir);
Error in tempname (line 17)
dirname = tempdir;
Error in savepath>iFopenTempLocation (line 274)
name = tempname;
Error in savepath (line 158)
[ fid, tempfilename ] = iFopenTempLocation( );
Error in cvx_setup (line 38)
stat = savepath;
------------------------------------------------------------------
Please report this error to support, and include entire output of
CVX_SETUP in your support request.
---------------------------------------------------------------------------de here
I used Run As Administrator for MATLAB and could so
I am trying to save images in Matlab by using the print function:
myImage = magic(500);
myFigure = figure('visible','off');
r = 1;
set(myFigure, 'PaperUnits', 'inches', 'PaperPosition', [0 0 1920 1080]/r);
% the program works fine on both computers without the line above
% however, the program runs fine on one computer only with this line
imagesc(myImage); axis image;
print(myFigure, '-dpng', sprintf('-r%d',r), 'myOutput.png');
When I run this program locally with Matlab R2012b, it works as expected. However, if I try to run it on a remote machine with Matlab R2011b, I get the following error message:
Error using ghostscript (line 188)
Problem converting PostScript. System returned error: -1.Failed to convert to
output format; Ghostscript status: -100.**** Unable to open the initial device,
quitting.
and the following error which is triggered by the line with the call to print():
Error in print>LocalPrint (line 311)
pj = ghostscript( pj );
Error in print (line 237)
LocalPrint(pj);
The code is more specifically:
if pj.GhostDriver
pj = ghostscript( pj );
elseif strcmp( pj.Driver, 'hpgl' )
hpgl( pj );
end
On my laptop, pj.GhostDriver is '' and pj.Driver is png.
One possible explanation is that I can launch ghostscript locally from the terminal:
$ ghostscript
GPL Ghostscript 9.10 (2013-08-30)
Copyright (C) 2013 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
GS>
but not remotely from the terminal (since I do not have any graphical interface on the remote computer):
$ ghostscript
GPL Ghostscript 9.05 (2012-02-08)
Copyright (C) 2010 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
GPL Ghostscript 9.05: Cannot open X display `(null)'.
**** Unable to open the initial device, quitting.
I would have thought Matlab already knew about it, since I launch Matlab on both computers by using:
matlab -nosplash -nodesktop -singleCompThread
Moreover, this problem is easily fixed as far as the terminal is concerned by adding the following line to ~/.bashrc:
export GS_DEVICE=display
Any hint at a solution?
I am working on matlab R2013a on Ubuntu. I am referring this code:
sift_bin = fullfile('lib/sift/bin/siftfeat');
[pf,nf,ef] = fileparts(filename);
desc_file = [fullfile(pf,nf) '.txt'];
im1=imread(filename);
if (size(im1,1)<=1000 && size(im1,2)<=1000)
status1 = system([sift_bin ' -x -o ' desc_file ' ' filename]);
else
status1 = system([sift_bin ' -d -x -o ' desc_file ' ' filename]);
end
But it gives an error:
lib/sift/bin/siftfeat cannot execute binary file
Is there anything wrong with system call?
lib/sift/bin/siftfeat is a path of sift library.
Use file utility to make sure that file is an executable and see its architecture
system('file /bin/ls')
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked
(uses shared libs), for GNU/Linux 2.6.24,
BuildID[sha1]=0xf31e99218b4d7034cf8257055686bca22f5a3c01, stripped
ans = 0
Then uname -a shows architecture of your system
system('uname -a')
Linux optiPlex7010 3.8.0-35-generic #50-Ubuntu SMP Tue Dec 3 01:24:59 UTC 2013
x86_64 x86_64 x86_64 GNU/Linux
ans = 0
As one can see I have 64-bit Linux and executable is also 64-bit. However, when it comes to 32-bit systems and executable support is backward compatible. That means 64-bit system may execute both 32-bit and 64-bit executables, but 32-bit system can execute only 32-bit executables.
From your comments I see that you are trying to launch 64-bit executable in 32-bit system, which is not capable of doing that. You should find 32-bit version of siftfeat or change your OS to 64-bit, if that is possible.
I have a series of experiments that were written for MATLAB, but recently we are trying to run them through Octave instead. I realize they are mostly compatible, but I have been running into a few problems, and none of the online FAQs or directions I have found have addressed these at all. It's complicated a bit because there are multiple .m files that interact; however, for now I am going to focus on the main program. Anyway, so when I try to run the file (MLP.m) through octave, I get the following errors in the Terminal window:
error: dir: expecting directory or filename to be a char array
error: called from:
error: /Applications/Octave.app/Contents/Resources/share/octave/3.2.3/m/miscellaneous/dir.m at line 128, column 5
error: /Applications/MATLAB_R2008a/toolbox/psychoacoustics/MLParameters.m at line 86, column 7
error: /Applications/MATLAB_R2008a/toolbox/psychoacoustics/MLP.m at line 9, column 3
The lines it is referencing are as follows:
1)
d = dir([cd myslash 'Experiments_MLP' myslash '*.m']);
2)
s = MLParameters;
What about these lines is incompatible with Octave? I can't find anything online that indicates that these won't work.
After that, the Terminal window gives me this batch of nonsense:
dyld: Library not loaded: /usr/X11/lib/libfreetype.6.dylib
Referenced from: /usr/X11R6/lib/libfontconfig.1.dylib
Reason: Incompatible library version: libfontconfig.1.dylib requires version 13.0.0 or later, but libfreetype.6.dylib provides version 10.0.0
dyld: Library not loaded: /usr/X11/lib/libfreetype.6.dylib
Referenced from: /usr/X11R6/lib/libfontconfig.1.dylib
Reason: Incompatible library version: libfontconfig.1.dylib requires version 13.0.0 or later, but libfreetype.6.dylib provides version 10.0.0
/Applications/Gnuplot.app/Contents/Resources/bin/gnuplot: line 71: 1077 Trace/BPT trap GNUTERM="${GNUTERM}" GNUPLOT_HOME="${GNUPLOT_HOME}" PATH="${PATH}" DYLD_LIBRARY_PATH="${DYLD_LIBRARY_PATH}" HOME="${HOME}" GNUHELP="${GNUHELP}" DYLD_FRAMEWORK_PATH="${DYLD_FRAMEWORK_PATH}" GNUPLOT_PS_DIR="${GNUPLOT_PS_DIR}" DISPLAY="${DISPLAY}" GNUPLOT_DRIVER_DIR="${GNUPLOT_DRIVER_DIR}" "${ROOT}/bin/gnuplot-4.2.6" "$#"
/Applications/Gnuplot.app/Contents/Resources/bin/gnuplot: line 71: 1083 Trace/BPT trap GNUTERM="${GNUTERM}" GNUPLOT_HOME="${GNUPLOT_HOME}" PATH="${PATH}" DYLD_LIBRARY_PATH="${DYLD_LIBRARY_PATH}" HOME="${HOME}" GNUHELP="${GNUHELP}" DYLD_FRAMEWORK_PATH="${DYLD_FRAMEWORK_PATH}" GNUPLOT_PS_DIR="${GNUPLOT_PS_DIR}" DISPLAY="${DISPLAY}" GNUPLOT_DRIVER_DIR="${GNUPLOT_DRIVER_DIR}" "${ROOT}/bin/gnuplot-4.2.6" "$#"
error: you must have gnuplot installed to display graphics; if you have gnuplot installed in a non-standard location, see the 'gnuplot_binary' function
I have GNUPlot installed, and I checked the gnuplot_binary function, which didn't give me any answers. GNUPlot is installed in my /Applications directory, along with Octave itself. Why shouldn't this work? The README file that came with GNUPlot didn't indicate a special directory for it to be installed in. What about the the dyld library not loaded errors? Is that related to the GNUPlot problem, or is it something else?
Anyway, thanks for your help
I know you already solved your problem, but if you have problems again here are some links with basic information about the differences between Matlab and Octave:
Porting programs from Matlab to Octave
Differences between Octave and MATLAB
Addressing your first error, it's easier to explain with an example:
dirName = '/some/path'; %# base directory
filesPath = fullfile(dirName, 'MLP', '*.m'); %# full path string
d = dir(filesPath); %# expand/enumerate files
for i=1:numel(d)
disp( d(i).name )
end
You also could have built the path using string concatenation yourself:
%# '/some/path/MLP/*.m'
filesPath = [dirName filesep 'MLP' filesep '*.m'];
The above should work for both MATLAB and Octave