Sevenless
Sevenless

Reputation: 2835

Shell variable name queried from Matlab has additional character

I'm working with the following script, run_test:

#!/bin/sh

temp=$1;
cat <<EOF | matlab

[status name] = unix('echo $temp');
disp(name);

% some Matlab code

test_complete = 1;
save(name)

exit
EOF

I want to pass a name to the script, run some code then save a .mat file with the name that was passed. However, there is a curious piece of behavior:

[energon2] ~ $ ./run_test 'run1'
Warning: No display specified.  You will not be able to display graphics on the screen.

                            < M A T L A B (R) >
                  Copyright 1984-2010 The MathWorks, Inc.
                Version 7.12.0.635 (R2011a) 64-bit (glnxa64)
                               March 18, 2011


  To get started, type one of these: helpwin, helpdesk, or demo.
  For product information, visit www.mathworks.com.

>> >> >> >> run1

>> >> >> >> >> 
[energon2] ~ $ ls *.mat
run1?.mat

There is a "?" at the end of the file name when it's saved, but not when displayed on command line. This is acceptable for my needs, but a bit irritating to not know why it's occurring. Any explanation would be appreciated.

Edits, solution: Yuk was correct below in the underlying cause and the use of save('$temp'). I'm now using the following script

#!/bin/sh

temp=$1;
cat <<EOF | matlab

% some Matlab code

test_complete = 1;
save('$temp')

exit
EOF

Thanks for the help.

Upvotes: 1

Views: 357

Answers (1)

yuk
yuk

Reputation: 19880

You name variable has end-of-line as the last character. When you run echo run1 in unix this command display run1 and then "hit enter". In your script all the output of echo are saved to the name variable.

You can confirm it with the following:

>> format compact
>> [status, name] = unix('echo run1')
status =
     0
name =
run1

>> numel(name)
ans =
     5
>> int8(name(end))
ans =
   10
>> int8(sprintf('\n'))
ans =
   10

Apparently this character can be a part of a file name in unix, but shell displays it as ?.

Can't you do save($temp) instead?

EDIT: See my comments below for correction and more explanation.

Upvotes: 3

Related Questions