Refactor last_path handling out of get_data_dir
authorLukáš Jiřiště <jiriste@icpf.cas.cz>
Mon, 14 Oct 2024 09:08:55 +0000 (11:08 +0200)
committerLukáš Jiřiště <jiriste@icpf.cas.cz>
Mon, 14 Oct 2024 09:08:55 +0000 (11:08 +0200)
This is done so that the logic can be used with function other than
uigetdir (eg. uigetfile)

get_data_dir.m
get_last_path.m [new file with mode: 0644]
update_last_path.m [new file with mode: 0644]

index 40d70434d64360bc47850f4e8dd396541c2c9345..82b7c4b6801738e49c26524b5f26a6473bb07d6d 100644 (file)
@@ -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 (file)
index 0000000..d9deab5
--- /dev/null
@@ -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 (file)
index 0000000..7511e9d
--- /dev/null
@@ -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