From 556eebfa837dbc403a581402ff3f9237cea1bb92 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ji=C5=99i=C5=A1t=C4=9B?= Date: Mon, 14 Oct 2024 11:08:55 +0200 Subject: [PATCH] Refactor last_path handling out of get_data_dir This is done so that the logic can be used with function other than uigetdir (eg. uigetfile) --- get_data_dir.m | 20 ++------------------ get_last_path.m | 12 ++++++++++++ update_last_path.m | 8 ++++++++ 3 files changed, 22 insertions(+), 18 deletions(-) create mode 100644 get_last_path.m create mode 100644 update_last_path.m diff --git a/get_data_dir.m b/get_data_dir.m index 40d7043..82b7c4b 100644 --- a/get_data_dir.m +++ b/get_data_dir.m @@ -1,24 +1,8 @@ function dir_path = get_data_dir() - if (isfile('.last_path')) - file_id = fopen('.last_path', 'r'); - last_path = char(fread(file_id))'; - fclose(file_id); - dir_path = uigetdir(last_path, 'Select the data folder'); - if ispc - fileattrib('.last_path', '-h'); - end - else - dir_path = uigetdir(pwd(), 'Select the data folder'); - end + dir_path = uigetdir(get_last_path(), 'Select the data folder'); if dir_path == 0 return end - - file_id = fopen('.last_path', 'w'); - fprintf(file_id, "%s", dir_path); - fclose(file_id); - if ispc - fileattrib('.last_path', '+h'); - end + update_last_path(dir_path) end diff --git a/get_last_path.m b/get_last_path.m new file mode 100644 index 0000000..d9deab5 --- /dev/null +++ b/get_last_path.m @@ -0,0 +1,12 @@ +function last_path = get_last_path() + if (isfile('.last_path')) + file_id = fopen('.last_path', 'r'); + last_path = char(fread(file_id))'; + fclose(file_id); + if ispc + fileattrib('.last_path', '-h'); + end + else + last_path = pwd() + end +end diff --git a/update_last_path.m b/update_last_path.m new file mode 100644 index 0000000..7511e9d --- /dev/null +++ b/update_last_path.m @@ -0,0 +1,8 @@ +function update_last_path(last_path) + file_id = fopen('.last_path', 'w'); + fprintf(file_id, "%s", last_path); + fclose(file_id); + if ispc + fileattrib('.last_path', '+h'); + end +end -- 2.30.2