Matlab from command line (-nodisplay -nojvm) segmentation violation on exit, on Mac OS

13 views (last 30 days)
Certain programs crash when I try to exit them from the command line (or call them within Python). (The weird thing is I have not been able to determine what causes some programs to crash and some not to.) So e.g. here is a very simple Matlab program (bugtest.m) which does crash:
function bugtest(ifile, ofile)
data = csvread(ifile, 1, 0); % skip the first line
csvwrite(ofile, data);
end
Now, when I call it from the command line, and exit separately, it works fine:
bash> /Applications/MATLAB_R2018b.app/bin/matlab -nodisplay -nojvm -r "bugtest('z2.csv','z3.csv')"
< M A T L A B (R) >
Copyright 1984-2018 The MathWorks, Inc.
R2018b (9.5.0.944444) 64-bit (maci64)
August 28, 2018
For online documentation, see https://www.mathworks.com/support
For product information, visit www.mathworks.com.
>> exit
exit
But what I actually want it to do is to exit as well, i.e. include the exit on the command line, and it crashes (depending on the program, but the program bugtest.m always crashes):
bash> /Applications/MATLAB_R2018b.app/bin/matlab -nodisplay -nojvm -r "bugtest('z2.csv','z3.csv');exit"
< M A T L A B (R) >
Copyright 1984-2018 The MathWorks, Inc.
R2018b (9.5.0.944444) 64-bit (maci64)
August 28, 2018
For online documentation, see https://www.mathworks.com/support
For product information, visit www.mathworks.com.
--------------------------------------------------------------------------------
Segmentation violation detected at Thu Aug 22 15:55:40 2019 +0930
--------------------------------------------------------------------------------
How do I fix this?
  3 Comments
Aakriti Suman
Aakriti Suman on 26 Mar 2020
Hi Peter,
Could you please send us the csv files that you are using in your MATLAB code? It will help us in investigating your query better.
PeterB
PeterB on 26 Mar 2020
Hi Aakriti, Thank you for replying.
First I should say that I found a workaround: by adding a pause(1) statement at the end of my Matlab program, the problem goes away. This was suggested when I asked on Stack Exchange here: https://stackoverflow.com/questions/57618313/matlab-command-line-some-programs-crash-on-exit
As for the test files... it was 10,000 lines of data. So rather than cut+paste, I will include here the Python program I used to generate similar data today. Simply run this to create z2.csv (it just writes a bunch of random numbers, and gives identical results with Python2 or Python3). This still gives the same error I reported back in last August, running Mac OS X 10.15.2, and (as you can see from the command line) Matlab R2018b.
import random
op = open("z2.csv", "w")
# creates header + 10000 rows of random csv data
random.seed(1)
# header
op.write("n,f1,f2,f3,f4,f5\n")
for n in range(10000):
row = []
op.write("%d," % (n+1)) # add 1 to index from 1
for k in range(5):
row.append("%.6f" % random.random())
op.write(",".join(row) + "\n")
op.close()

Sign in to comment.

Answers (1)

Aakriti Suman
Aakriti Suman on 27 Mar 2020
Edited: Aakriti Suman on 27 Mar 2020
I understand that you are not able to execute a user defined function and the 'exit' command one after the other at once through the command line.
I tried to execute a simple script as below.
function bugtest(a)
sum = a+2;
disp(sum)
end
Through the command line, I called the above function in two ways:
  • >>bugtest(2)
>>exit
  • >>bugtest(2);exit
In both the above ways, my MATLAB did not crash and the MATLAB window exited normally.
Since you are using python as a third party tool, there is a slight possibility that the usage of both the tools simulataneously is not being used correctly.
I also see that you are using R2019 version of MATLAB. 'csvread' function has compatibility issues with MATLAB R2019a and later versions.
You can use 'readmatrix' instead of 'csvread' function. I am attaching its documentation link for your reference.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!