On Mac- How to use shell commands (or AppleScript) using MATLAB variables?

3 Ansichten (letzte 30 Tage)
Hello. Just to preface- I use image processing on MATLAB (on a Mac) and I have practically no experience with shell commands or Applescript, other than with this little adventure. So if you have an idea of how to solve my problem, please keep this in mind.
I am trying to run a MATLAB script for segmenting MRI scans, and I need to be able to open scans in another application (MRIcron) quickly. I need to be able to do this in a while loop, as I have many scans that I will need to open. For Windows, I understand this is easy because you can just use the command
! image.hdr &
in your current working directory, and it will open it with the correct application and be able to change directory as your MATLAB directory changes. Unfortunately, it is not as simple on a Mac. I am able to do the same on a Mac with the system function or the following command:
! osascript -e "tell application \"mricron\"" -e "open \"filepath:scans:subject1:study1:image.hdr\"" -e "end tell"
The thing is, I need to be able to change the subject and study names (which I have created MATLAB variables for) on each loop, and a shell command won't recognize MATLAB variables. It also does not seem to have the same path as the current working directory in MATLAB. I also attempted to create an Applescript that should, in theory be able to accept an input (such as a MATLAB variable?) but that doesn't seem to work:
on run argv
tell application "mricron"
activate
set subject_study_path to (item 1 of argv)
open subject_study_path
end tell
end run
Where the MATLAB command to run this Applescript (mricronopen.scpt) with the path to the current loop's file (fl_path) is:
status = system('osascript /Users/jacksonsn/Documents/mricronopen.scpt fl_path')
So- I am able to open the application MRIcron, but it does not open the correct file unless I type in the entire path by hand (which is not reasonable in a while loop where I have over 50 scans to look at). I think solutions to the following problems could solve the issue, or if anyone has any other ideas, please let me know!
  1. Can I get osascript to recognize a MATLAB variable in a shell command as part of the path name?
  2. Is there a way to pass a variable from MATLAB (that contains a string) to Applescript and have Applescript recognize the string the variable contains?
  3. Is there another way to change the path that the terminal uses, to match the current working directory in MATLAB?
  4. Is there any other way to open a file with a specific application in MATLAB using a variable to store the file path?
Please let me know if you have any additional questions!
Thank you!!

Antworten (1)

Walter Roberson
Walter Roberson am 28 Dez. 2018
use sprintf to construct the command into aa character vector and system() the character vector

Community Treasure Hunt

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

Start Hunting!

Translated by