compiled matlab script where incorrect variables are passing

6 ビュー (過去 30 日間)
nines
nines 2025 年 7 月 8 日
コメント済み: Star Strider 2025 年 7 月 9 日
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 件のコメント
nines
nines 2025 年 7 月 9 日
thank you so much! you're the best!!
Star Strider
Star Strider 2025 年 7 月 9 日
As always, my pleasure!
I very much appreciate your compliment!

サインインしてコメントする。

その他の回答 (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