Using Matlab to open a text file

34 views (last 30 days)
Pete
Pete on 14 Jul 2022
Edited: Pete on 14 Jul 2022
Hi all,
Running MacOS Mojave and have set the default terminal to ZSh (I think!)
I've a script that returns a list of text files in cell format and want to give user the option to open (some of) them.
Without Matlab, I navigate to the folder and open the text file by double clicking. With me here?! This opens the file in Sublime Text (or other text editer, but ST is my chosen default). Alternatively, I could use the Mac OS terminal to open it... Lets build the story here - sorry to those who already know this stuff.
I can use the terminal to open Sublime Text (or other application that exists) by typing
subl
(or whatever application I want to open) into the command window.
I can open a specified text file from the terminal window in a similar way by typing
subl NameOfTextFile.txt
I'm wanting to essentially have Matlab do this, by essentially running
system('subl NameOfTextFile.txt')
but as seen when I run as follows:
system('subl NameOfTextFile.txt')
/bin/bash: subl: command not found
ans =
127
I can successfully run other system commands from the Matlab command window, for example
system('sleep 4')
will pause the command window for 4 seconds, so I know that things 'should' work if correctly set, but obviously I'm not setting things correctly somewhere.
I've also tried
unix('subl NameOfTextFile.txt')
/bin/bash: subl: command not found
ans =
127
but as seen, same issue. The 'unix('sleep 4')' also works as I'd expect.
Is the issue with my chosen system shell (zsh rather than bash) in which case, how can I set it to work, or something else?
TIA

Accepted Answer

Benjamin Thompson
Benjamin Thompson on 14 Jul 2022
Looks like subl is not in your search path. You can use the system command to print out the search path that the MATLAB process sees. If it is not there, then add it. I don't use UNIX so I don't know the specific commands for that.
  1 Comment
Pete
Pete on 14 Jul 2022
Edited: Pete on 14 Jul 2022
Thanks Ben,
Above what I initially understood here, but think I understand what I've done.
By adding the link to Sublime Text in the path which is defined by
getenv('PATH')
I noted that there was no 'file' titled "SUBL" within the directories returned by the above command.
So by running
sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/bin/subl
in the TERMINAL rather than command window, I've successfully added the SUBL file to a location which occurs in the path. Again, initially I didn't understand what is going on here, but now think I've got a grasp into what these bits are. Credit to above also on:
In conclusion then,
CommandToOpen = sprintf('subl %s', FilePathToOpen);
system(CommandToOpen);
now works and opens the file that I select via a quest_dlg for further processing.
Thanks again

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!