从 linux 的命令行启动 matlab 时,我的行为很奇怪。
我在 linux 中有一个 bash 脚本,它从命令行在 matlab 中执行一个函数,并使用用 C++ 编写的自定义函数执行其他操作,如下所示:
#!/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"
脚本完成后,尽管命令有效,但我看不到终端中正在写的内容。我需要到reset
终端。
事实是,如果我只使用启动 matlab 一切正常,prepare_data_matlab( 'A' )
但是当我使用 option 执行函数时问题就来了prepare_data_matlab( 'B' )
。
我逐行注释,发现问题B
出在调用函数的选项上
dlmwrite(file_name, B, ' ');
中未使用prepare_data_matlab( 'A' )
。
那么,我应该如何从命令行执行 matlab 以避免这种行为?该功能是否存在已知错误dlmwrite()
?
我正在使用Ubuntu 12.04 64 位、GNU bash、版本4.2.24(1)-release (x86_64-pc-linux-gnu) 和matlab2011a。
编辑:生成的输出prepare_data_matlab( 'A' )
是
生成的输出prepare_data_matlab( 'B' )
是
EDITED: file_name
创建为strcat(path_to_data,f);
wherepath_to_data = /tmp/
和f = data_out.txt
. 矩阵B
不显示前后。
从 bash 脚本生成 MATLAB 脚本之前或之后的唯一输出到终端,如下所示:
echo "#### SELECT DATA FROM WORKSPACE ####"
matlab2011a -nodesktop -nosplash -r "prepare_data_matlab( 'B' ); quit";
echo "#### Process Data as input in a C++ programs ####"
MATLAB 函数从工作区中选择数据并将其保存到磁盘,如下所示:
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
已编辑: 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
工作区中有更多数组,但那些是我加载的。
该prepare_data_matlab
函数与上面发布的相同,但参数错误检查如下:
%% 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
和以下命令:
cd /data/matlab;
A
在两种情况下(选项和选项)的参数错误检查之后执行B
,即在if
语句之前执行。