5

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 enter image description here

The output generated for prepare_data_matlab( 'B' ) is enter image description here

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 Aand option B), that is, before the if statement.

pQB
  • 3,077
  • 3
  • 23
  • 49
  • 4
    redirect the output to a file (shell redirection via `>`) and let us know what output is generated that corrupts the terminal. – mnagel Jul 31 '13 at 12:23
  • @mnagel I have updated the question with the output generated by the matlab command. I did not notice about the strange characters and I have no idea what they mean. – pQB Aug 01 '13 at 07:13
  • Does the `"#### SELECT DATA FROM WORKSPACE ####"` message appear correctly on your screen? – cabad Aug 05 '13 at 14:25
  • @pQB: what is your goal here and what exactly is the problem? if you want to ignore any output from MATLAB to the standard output simply launch it as: `matlab ... > /dev/null 2>&1` – Amro Aug 05 '13 at 14:53
  • @cabad Yes, the messages from the bash script appear correctly on the screen – pQB Aug 05 '13 at 15:32
  • @Amro The problem is that I do not see what I type in the console when the script finishes. In fact, if I run only the MATLAB script the same happens. The goal is to find what is wrong so I do not need type `reset` each time the script finishes. – pQB Aug 05 '13 at 15:35
  • To be as precise as possible, If I comment the line `dlmwrite(filename, B, ' ');` the problem disappears. – pQB Aug 05 '13 at 15:41
  • Are you sure that `filename` is correct? I still doubt that `dlmwrite` is the cause of the problem, but if it is you can replace it with `fprintf` with little effort. See here for an example: http://stackoverflow.com/a/6540984/97160 – Amro Aug 05 '13 at 15:47
  • @amro Yes, It's correct, I've checked it by opening when the script finishes (and removing it before it start :). I'll give a try to `fprintf` – pQB Aug 05 '13 at 15:52
  • this might be a better example for `fprintf`: http://stackoverflow.com/a/17335675/97160 – Amro Aug 05 '13 at 15:54
  • @amro Good point. I saved the data with fprintf as you suggested and the same happens (however is much faster :). Now I'm totally lost as dlmwrite is not the root of the problem. – pQB Aug 05 '13 at 16:26

2 Answers2

5

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.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
cabad
  • 4,555
  • 1
  • 20
  • 33
  • Data is read from a MATLAB workscape. No output is written to the terminal from the MATLAB function before or after the `dlmwrite` call. I've updated the question with this information. – pQB Aug 05 '13 at 10:15
  • I changed my answer since I did some searching and found this to be a bug in MATLAB. As such, you cannot fix it, but you can get around it as suggested in my new answer. – cabad Aug 05 '13 at 16:33
  • You are right. Your suggestion solved the problem. So much error checking at all ... – pQB Aug 05 '13 at 16:38
  • 1
    huh, nice find +1. I would have never figured this out from my Windows machine :) This bug should be reported to MATLAB: http://www.mathworks.com/support/bugreports/ – Amro Aug 05 '13 at 16:40
  • 2
    @Amro From MathWork Support: 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. – pQB Aug 09 '13 at 07:50
  • 1
    @pQB: I see, thanks for letting us know. cabad: you should add the reply from TMW to your answer – Amro Aug 09 '13 at 12:18
1

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.

Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454
  • I tried redirect the output to the /dev/null and nothing is printed, neither the banner printed at the beginning. However, the problem persist. When the script finishes I do not see what I type in the linux console. – pQB Aug 05 '13 at 15:37
  • if you are not printing anything from MATLAB to the standard output, then perhaps its your C++ program that is corrupting the terminal? – Amro Aug 05 '13 at 15:39
  • I dont think so. I execute `matlab2011a -nodesktop -nosplash -r "prepare_data_matlab( 'B' ); quit";` from the linux bash terminal and the problem persists, but disappears if I comment the `dlmwrite` call within the prepare_data function. – pQB Aug 05 '13 at 15:43
  • is `prepare_data_matlab` exactly the same function posted in your question? also would help if you tell us what are the matrices `A` and `B`. Run `whos -file workspace_with_my_arrays.mat` and post the output please.. – Amro Aug 05 '13 at 15:49
  • according to the above, A and B are multi-dimensional rather than 2D matrices. How do you want it to be written as ASCII file? – Amro Aug 05 '13 at 16:26
  • My apologies. I cleaned the next line -> `y = reshape( permute(B, [2 1 3]), size(B,1) * size(B,2), size(B,3));` before paste the code in the question (I simply renamed some variables and deleted extra comments) – pQB Aug 05 '13 at 16:32
  • Thank you for your effort but the problem is solved with the nohup answer. Especially thank for the fprintf suggestion :) – pQB Aug 05 '13 at 16:39