開いているスクリプト​のあるフォルダのパス​を得る方法

44 views (last 30 days)
Yuki Koyama
Yuki Koyama on 14 May 2020
Edited: Hiroki Okawa on 14 May 2020
whichを用いることでそのスクリプトの名前を引数開いているスクリプトのパスを取得できますが,そのスクリプト名がパスに含まれます.
そのスクリプト名を含まない,そのフォルダまでのパスを取得する方法はありますか?
たとえばC:\Users\name\matlab\script.mlxに関して,script.mlxがあるフォルダのパス
C:\Users\name\matlab\を取得する方法が知りたいです.

Accepted Answer

Hiroki Okawa
Hiroki Okawa on 14 May 2020
Edited: Hiroki Okawa on 14 May 2020
fileparts関数はいかがでしょうか。
file = 'H:\user4\matlab\myfile.txt'
[filepath,name,ext] = fileparts(file)
% 以下出力
% filepath = 'H:\user4\matlab'
% name = 'myfile'
% ext = '.txt'

More Answers (1)

Kenta
Kenta on 14 May 2020
こんにちは、extractBefore関数を使うのはいかがでしょうか?以下のようにすればできると思います
path=which(script_name);
extractBefore(script_name)

Categories

Find more on MATLAB 入門 in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!