compiled matlab script where incorrect variables are passing

6 次查看(过去 30 天)
Hello,
I compiled a matlab script and am submitting it as a job in bash:
#!/bin/bash
# Generates and submits one SLURM job per slice, using the compiled
# TSE_multispinecho_EPG executable + MATLAB Runtime.
# ------------- USERADJUSTABLE PARAMETERS --------------------------
PROJECT_DIR="/path/to/dir"
PROJECT_NAME="project"
SLICE=1
SUBJECT_CODE='subj1'
EXEC="$PROJECT_NAME/bin/TSE_multispinecho_EPG" # compiled executable
# -------------------------------------------------------------------
cat > "$JOB" <<EOF
#!/bin/bash
echo " slice : ${SLICE}"
"${EXEC}" "${PROJECT_DIR}" "${PROJECT_NAME}" "${SUBJECT_CODE}" "${SLICE}"
EOF
# ---- submit -----
sbatch "$JOB"
done
and then the beginning of the matlab functions looks like:
function TSE_multispinecho_EPG(project_directory, project_name, subject_code, slice);
%% TSE multi spin echo EPG
fprintf('slice within function: %d\n', slice);
and the slice number should be 1. When I run the bash command, I get '1', but when it gets the matlab part of the script I am getting '49'.
I cleared the cache, I made sure there weren't any extra slice variables causing renaming issues.
I can run the non-compiled command just fine, and then slice 1 will be printed correctly:
matlab -nodisplay -nosplash -r "TSE_multispinecho_EPG("/projectdir", "projectname", "subj01", "1")"
Any help would be much appreciated,
Thanks.

采纳的回答

Star Strider
Star Strider 2025-7-8
编辑:Star Strider 2025-7-8
The '49' is the ASCII code for '1' --
Q = double('1')
Q = 49
Mystery solved!
You may need to change parts of your code, or perhaps use the str2double function --
N = str2double('1')
N = 1
It all depends on what you want to do, and the result you want.
EDIT -- (8 Jul 2025 at 19:26)
The problem may be here:
slice = '1';
fprintf('slice within function: %d\n', slice)
slice within function: 49
so one option could be --
fprintf('slice within function: %s\n', slice);
slice within function: 1
.
  3 个评论

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2025-7-8
Note that
double('1')
ans = 49
so somehow you are getting a quoted 1 instead of a numeric 1.
I would suggest trying
matlab -nodisplay -nosplash -r "TSE_multispinecho_EPG("/projectdir", "projectname", "subj01", 1)"
  1 个评论
Walter Roberson
Walter Roberson 2025-7-8
Ah, I did not notice that the executable was compiled. Compiled executables always receive their parameters as strings, never as actual numeric values.

请先登录,再进行评论。

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by