Shell variable name queried from Matlab has additional character - matlab

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.

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.

Related

OSError: This docstring was not generated by Nipype

Hey i am runing the following piece of code:
import nipype.interfaces.spm as spm
realign = spm.Realign()
And getting the following error:
raise IOError("This docstring was not generated by Nipype!\n") from e
OSError: This docstring was not generated by Nipype!
After debugging:
My code:
spm.Realign()
When this runs it uses matlab to run the follwing (base.py lines 217):
mlab.inputs.script = """
if isempty(which('spm')),
throw(MException('SPMCheck:NotFound','SPM not in matlab path'));
end;
spm_path = spm('dir');
[name, version] = spm('ver');
fprintf(1, 'NIPYPE path:%s|name:%s|release:%s', spm_path, name, version);
exit;
"""
try:
out = mlab.run()
I run it in Matlab and got :
>> isempty(which('spm'))
ans =
logical
0
>> [name, version] = spm('ver');
fprintf(1, 'NIPYPE path:%s|name:%s|release:%s', spm_path, name, version);
NIPYPE path:C:\Program Files\MATLAB\R2022b\toolbox\spm12\spm12|name:SPM12|release:7771
so as you can see I have result from this script, but when checking the "out" variable here (line 239):
out = sd._strip_header(out.runtime.stdout)
Stdout is "" empty string
So I think the problem is Somehow related to the answer of the script from Matlab, how to fix it ??
Thanks!

Save job output from SDSF into a PDS and using ISPF functions in REXX

We periodically runs jobs and we need to save the output into a PDS and then parse the output to extract parts of it to save into another member. It needs to be done by issuing a REXX command using the percent sign and the REXX member name as an SDSF command line. I've attempted to code a REXX to do this, but it is getting an error when trying to invoke an ISPF service, saying the ISPF environment has not been established. But, this is SDSF running under ISPF.
My code has this in it (copied from several sources and modified):
parse arg PSDSFPARMS "(" PUSERPARMS
parse var PSDSFPARMS PCURRPNL PPRIMPNL PROWTOKEN PPRIMCMD .
PRIMCMD=x2c(PPRIMCMD)
RC = isfquery()
if RC <> 0 then
do
Say "** SDSF environment does not exist, exec ending."
exit 20
end
RC = isfcalls("ON")
Address SDSF "ISFGET" PPRIMPNL "TOKEN('"PROWTOKEN"')" ,
" (" VERBOSE ")"
LRC = RC
if LRC > 0 then
call msgrtn "ISFGET"
if LRC <> 0 then
Exit 20
JOBNAME = value(JNAME.1)
JOBNBR = value(JOBID.1)
SMPDSN = "SMPE.*.OUTPUT.LISTINGS"
LISTC. = ''
SMPODSNS. = ''
SMPODSNS.0 = 0
$ = outtrap('LISTC.')
MSGVAL = msg('ON')
address TSO "LISTC LVL('"SMPDSN"') ALL"
MSGVAL = msg(MSGVAL)
$ = outtrap('OFF')
do LISTCi = 1 to LISTC.0
if word(LISTC.LISTCi,1) = 'NONVSAM' then
do
parse var LISTC.LISTCi . . DSN
SMPODSNS.0 = SMPODSNS.0 + 1
i = SMPODSNS.0
SMPODSNS.i = DSN
end
IX = pos('ENTRY',LISTC.LISTCi)
if IX <> 0 then
do
IX = pos('NOT FOUND',LISTC.LISTCi,IX + 8)
if IX <> 0 then
do
address ISPEXEC "SETMSG MSG(IPLL403E)"
EXITRC = 16
leave
end
end
end
LISTC. = ''
if EXITRC = 16 then
exit 0
address ISPEXEC "TBCREATE SMPDSNS NOWRITE" ,
"NAMES(TSEL TSMPDSN)"
I execute this code by typing %SMPSAVE next to the spool output line on the "H" SDSF panel and it runs fine until it gets to this point in the REXX:
114 *-* address ISPEXEC "TBCREATE SMPDSNS NOWRITE" ,
"NAMES(TSEL TSMPDSN)"
>>> "TBCREATE SMPDSNS NOWRITE NAMES(TSEL TSMPDSN)"
ISPS118S SERVICE NOT INVOKED. A VALID ISPF ENVIRONMENT DOES NOT EXIST.
+++ RC(20) +++
Does anyone know why it says I don't have a valid ISPF environment and how I can get around this?
I've done quite a bit in the past with REXX, including writing REXX code to handle line commands, but this is the first time I've tried to use ISPEXEC commands within this code.
Thank you,
Alan

Gnuplot prints differently when piping

I have file like this:
662.0,,9624
663.0,,9625
771.0,9624,
772.0,,9626
912.0,9625,
913.0,,9627
1083.0,9626,
1083.0,,9628
1174.0,9627,
1175.0,,9629
And when I use gnuplit console, it prints as expected:
set datafile separator ',';
plot 'data-exchange.log' using 1:2 pt 7 ps 2, '' using 1:3 pt 7 ps 2;
However, when I do the same when piping, it doesn't plot as expected:
cat data-exchange.log | gnuplot -p -e 'set datafile separator ","; plot "<cat" using 1:2 pt 7 ps 2, "" using 1:3 pt 7 ps 2;'
Not sure why is that...

How to combine outputs of multiple tasks performed using Matlab SGE?

I have the following batch file launching some m-files (main.m and f.m which are scripts) 4 times (4 tasks).
#$ -S /bin/bash
#$ -l h_vmem=2G
#$ -l tmem=2G
#$ -cwd
#$ -j y
#Run 4 tasks where each task has a different $SGE_TASK_ID ranging from 1 to 4
#$ -t 1-4
#$ -N example
#Output the Task ID
echo "Task ID is $SGE_TASK_ID"
cat main.m f.m | matlab -nodisplay -nodesktop -nojvm -nosplash
At the end I obtain 4 outputs that are example.o[...].1,example.o[...].2, example.o[...].3, example.o[...].4. Each of them looks like
...
Task ID is ...
< M A T L A B (R) >
...
>> >> >> >> >> >> >> >> >> >> >>
output =
4.0234 -3.4763
How can I combine these 4 outputs in a matrix 4x2 and save it?
You should save the relevant output from within f.m using MATLAB's save or something similar.
If you use the -r flag to call main and f from the command line you can add a variable which will contain the task ID and you can then access that from within f.m
matlab -nodisplay -nodesktop -nojvm -nosplash -r "main; ID = $SGE_TASK_ID; f; exit"
Then within f.m
% You theoretically generate some numeric result
result = rand(1, 2);
filename = sprintf('Result.%d.mat', ID);
save(filename, 'result')
This will save Result.0.mat, Result.1.mat etc.
Alternately, you could modify f.m such that it loads the data from the file, appends to it, and re-saves it every time
result = rand(1,2);
filename = 'Results.mat';
% If this is the first task, then create a new file, otherwise append to the old
if ID == 1
data = result;
else
tmp = load(filename, '-mat');
data = tmp.data;
data(ID,:) = result;
end
save(filename, 'data')

Ghostscript error converting multi-page PS to multi-page PDF using pdfwrite

I'm using Matlab to output a multi-page PS file:
print(figure, '-dpsc2', fullfile(folder, [file '.ps']), '-r600', '-append')
and then using Matlab to invoke Ghostscript to convert the resulting PS file to a PDF:
system(['"' gsPath '" -sDEVICE=pdfwrite \
-dDEVICEWIDTHPOINTS=' num2str(int32(width*72)) ' \
-dDEVICEHEIGHTPOINTS=' num2str(int32(height*72)) ' \
-dPDFFitPage \
-o "' fullfile(folder, [file '.pdf']) '" "' fullfile(folder, [file '.ps']) '"']);
which is just a really hard-to-read way of writing something along the lines of
gswin64c -sDEVICE=pdfwrite ^
-dDEVICEWIDTHPOINTS=100 ^
-dDEVICEHEIGHTPOINTS=100 ^
-dPDFFitPage ^
-o "C:\folder\output.pdf" "C:\folder\input.ps"
where I've put in example values for device dimensions and input/output paths. When I use this code to print a single figure (one page) to PDF, everything works perfectly. However, when printing multiple figures (multiple pages) to PDF, Ghostscript throws an error:
GPL Ghostscript 9.06 (2012-08-08)
Copyright (C) 2012 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
**** Unable to open the initial device, quitting.
Now, if I remove the -dDEVICEWIDTHPOINTS=100 -dDEVICEHEIGHTPOINTS=100 part of my Ghostscript command and again attempt to print multiple figures to a PDF, it works fine (except for the page size being different than what I want).
GPL Ghostscript 9.06 (2012-08-08)
Copyright (C) 2012 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Loading NimbusSanL-Regu font from %rom%Resource/Font/NimbusSanL-Regu... 4032872 2490784 2311720 1014184 2 done.
Has anyone else run into a similar problem and found a workaround for this issue? One of the keys here is that I need to be able to control the page size of the PDF produced. Thanks!
Below is an example that should run fine. First we create a multi-page PS file:
fname = 'test';
if exist([fname '.ps'], 'file'), delete([fname '.ps']); end
hfig = figure;
for i=1:10
plot(cumsum(rand(100,1)-0.5))
drawnow
print(hfig, '-dpsc2', '-append', [fname '.ps'])
end
close(hfig)
Next we convert it to PDF using Ghostscript, and properly crop the figures:
gs_path = 'C:\Program Files\gs\gs9.07\bin\gswin64c.exe';
gs_opts = '-dBATCH -dNOPAUSE -q';
% ps2pdf
cmd = sprintf('"%s" %s -sDEVICE=pdfwrite -dPDFFitPage -o %s %s', ...
gs_path, gs_opts, [fname '.pdf'], [fname '.ps']);
disp(cmd); system(cmd);
% get bbox
cmd = sprintf('"%s" %s -sDEVICE=bbox %s', ...
gs_path, gs_opts, [fname '.pdf']);
disp(cmd); [~,out] = system(cmd);
out = textscan(out, '%s', 'Delimiter','');
bbox = regexp(out{1}, '^%%BoundingBox: (\d+) (\d+) (\d+) (\d+)','tokens','once');
bbox = str2double(vertcat(bbox{:}));
bbox = [min(bbox(:,1:2)) max(bbox(:,3:4))];
% crop to bounding box
cmd = sprintf(['"%s" %s -o %s -sDEVICE=pdfwrite' ...
' -dDEVICEWIDTHPOINTS=%d -dDEVICEHEIGHTPOINTS=%d -dFIXEDMEDIA' ...
' -c "<</PageOffset [-%d -%d]>> setpagedevice" -f %s'], ...
gs_path, gs_opts, [fname '_cropped.pdf'], ...
bbox(3)-bbox(1), bbox(4)-bbox(2), bbox(1), bbox(2), [fname '.pdf']);
disp(cmd); system(cmd);