Using Matlab to open a text file

30 次查看(过去 30 天)
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

采纳的回答

Benjamin Thompson
Benjamin Thompson 2022-7-14
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 个评论
Pete
Pete 2022-7-14
编辑:Pete 2022-7-14
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by