I'm having a weird behaviour when launching matlab from the command line in linux.
I've a bash script in linux that execute a function in matlab from the command line and does other operations with custom functions written in C++ as follows:
#!/bin/bash
# prepare input data just to be sure it has not been written by other test!
matlab2011a -nodesktop -nosplash -r "prepare_data_matlab( 'A' ); quit"
# launch C++ program
...
# prepare more data
matlab2011a -nodesktop -nosplash -r "prepare_data_matlab( 'B' ); quit"
When the script is finished I can not see what I'm writing in the terminal, although the commands have effects. I need to reset
the terminal.
The fact is that everything works fine if I only launch matlab with the prepare_data_matlab( 'A' )
but the problem comes when I execute the function with option prepare_data_matlab( 'B' )
.
I have commented line by line and found that the problem is with option B
that call the function
dlmwrite(file_name, B, ' ');
which is not used in prepare_data_matlab( 'A' )
.
So, how should I execute the matlab from the command line to avoid this behaviour? Is there a known bug with the dlmwrite()
function?
I'm using Ubuntu 12.04 64 bits, GNU bash, versión 4.2.24(1)-release (x86_64-pc-linux-gnu) and matlab2011a.
EDITED: The output generated for prepare_data_matlab( 'A' )
is
The output generated for prepare_data_matlab( 'B' )
is
EDITED: file_name
is created as strcat(path_to_data,f);
where path_to_data = /tmp/
and f = data_out.txt
. Matrix B
is not displayed before or after.
The only output to the terminal before or after the MATLAB script is generated from the bash script as follow:
echo "#### SELECT DATA FROM WORKSPACE ####"
matlab2011a -nodesktop -nosplash -r "prepare_data_matlab( 'B' ); quit";
echo "#### Process Data as input in a C++ programs ####"
The MATLAB function select data from the workscape and save it to disk as follows:
function [ ] = prepare_data_matlab( type )
if strcmp(type,'A')
% load data from workscape
load ('workspace_with_my_arrays.mat', 'A');
% save data as a standalone variable
save('/tmp/A.mat', 'A');
elseif strcmp(type,'B')
% load data from workscape
load ('workspace_with_my_arrays.mat', 'B');
path_to_data = '/tmp/';
f = 'data_out.txt';
file_name = strcat(path_to_data,f);
% save data as a txt file
dlmwrite(file_name, B, ' ');
end
end
EDITED: whos -file workspace_with_my_arrays.mat
Name Size Bytes Class Attributes
A 610x340x103 170897600 double
B 610x340x103 170897600 double
P 610x340 1659200 double
t1 38855x100 31084000 double
t2 3921x2x100 6273600 double
There are more arrays in the workspace but those are which I load.
The prepare_data_matlab
function is the same as posted above but with an argument error checking as follow:
%% Load data from file
% Data is saved in a MATLAB variable or in TXT
if nargin ~= 1
error('Use: prepare_data_matlab( [ A | B ] )')
end
and the following command:
cd /data/matlab;
which is executed after the arguments error check in both cases (option A
and option B
), that is, before the if
statement.
To start MATLAB® on Linux platforms, type matlab at the operating system prompt. If you did not set up symbolic links in the installation procedure, then type matlabroot /bin/matlab . matlabroot is the name of the folder in which you installed MATLAB.
To run a MATLAB script from the the command line, use MATLAB's -r option, as in this example which runs the Matlab script my_simulation. m from the current directory. Note that the MATLAB script that you run should have an exit command in it.
In order to run a script you can open Matlab (you can prevent run it without the GUI using -nodisplay and -nodesktop flags), then run the script using the run command, and finally close matlab using exit . How do I open Matlab in terminal? type on terminal: cd /Applications/MATLAB_R2018b. app/bin. type on terminal: ./matlab.
- If you are correctly specifying the MATLAB installation script in the command line, this error indicates that your operating system is not configured properly, and cannot run the installer application. Usually, this is accompanied by errors when installing or using other applications on the system as well.
Several MATLAB functions enable you to control the placement of missing values before further processing. For example, use the 'MissingPlacement' option with the sort function to move NaN s to the end of the data. Even if you do not explicitly create missing values in MATLAB, they can appear when importing existing data or computing with the data.
Add matlab alias (copy the line towards the end of bashrc file) alias matlab=”/usr/local/MATLAB/R2021a/bin/matlab” … Source the bashrc file. … Now to open matlab, just type on terminal. How do I run MATLAB code? In MATLAB Online, the Run and Advance button is located in the Section section. Run all the code in the file.
The problem is not with dlmwrite
. This seems to be a bug in some versions of MATLAB, as reported in this link.
The proposed solution (if you have a buggy version of MATLAB) is to use nohup
:
nohup matlab -nodesktop -nosplash -r ...........
UPDATE: Per @Amro 's suggestion, @pQB reported the problem to MathWorks Support. Their response was:
The problem is a known issue in versions prior to R2012a. Run MATLAB under a different shell. For example, neither tcsh or zsh have this issue.
OLD answer:
The problem is not with dlmwrite
, but with the content of your matrix. Furthermore, unless file_name
points to stdout
(e.g., file_name='/dev/stdout';
), the dlmwrite
function will not write anything to screen and will not mess your terminal. Either file_name
points to stdout
or you are displaying the matrix B right before (or after) the dlmwrite
call.
In any case, the problem is with the contents of your matrix B (see the strange characters in your output). You need to fix the problem with your matrix B. Perhaps the method you are using to read its input data is faulty.
If you want to ignore output from MATLAB (like the banner printed at the beginning), launch the process and redirect both the standard input and error to /dev/null
device:
#!/bin/sh
echo '### running MATLAB ###'
matlab -nodesktop -nosplash -r "..." > /dev/null 2>&1
echo '### done ###'
./other_script.sh
matlab -nodesktop -nosplash -r "..." > /dev/null 2>&1
Note that you should be careful since MATLAB process returns immediately possibly before it has finished running, which could cause problems if your next program depends on files produced by MATLAB. See here for a possible solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With