compiled matlab script where incorrect variables are passing

4 views (last 30 days)
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.

Accepted Answer

Star Strider
Star Strider on 8 Jul 2025
Edited: Star Strider on 8 Jul 2025
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 Comments
Star Strider
Star Strider on 9 Jul 2025
As always, my pleasure!
I very much appreciate your compliment!

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 8 Jul 2025
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 Comment
Walter Roberson
Walter Roberson on 8 Jul 2025
Ah, I did not notice that the executable was compiled. Compiled executables always receive their parameters as strings, never as actual numeric values.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!