How to Open a Folder

Hey.
I need help trying to open a folder on matlab while the code is running. I want the folder to open as a response to the user clicking on the screen. I already have the folder in the same pathway as the overall code, I'm just having trouble with the syntax and general terminology to open a specfic folder.

11 Comments

Jan
Jan on 11 Apr 2021
Please explain what you call "open a folder"? Do you want to access its contents by using fopen, or do you want to show it in a file explorer, e.g. the Windows Explorer?
Oh yes, I mean access its content, so if the folder's name is PuzzleOne, how would I write the code for that exactly?
Jan
Jan on 11 Apr 2021
I still cannot guess, what "access it's contents" means...
basically they are gonna press on the screen and I want a folder to pop up and open without them having to open the folder manually, the code would do it for them if that makes sense.
project = 'PuzzleOne';
dinfo = dir(project);
filenames = fullfile(project, {dinfo.name});
Jan
Jan on 12 Apr 2021
@Ghina Alhunaidi: Remember, that the readers do not have the faintest idea about what you want to achieve. So explain it to us as if we were rubber ducks.
"basically they are gonna press on the screen" - who is "they" and what is "pressing on the screen"?! Do you mean: push a button in a GUI?
"and I want a folder to pop up and open without them having to open the folder manually" - "pressing on a screen" sounds like a manual opening.
A folder is a abstract construction for the organization of files. I'm sure you mean something very specific when you say "a folder pops up". So please explain explicitly, what this is. A window of the Windows Explorer? A dialog for choosing files like uigetfile? The output of dir in the command window?
Okay so I resolved the "clicking issue" and I'm using this line of code: ( [file,path] = uigetfile('*.mov'); ) which shows me all the folders in my desktop and allows me to click on the selected type of file:
And this allows me to open the "Window View" folder and see its contents like so:
But I'm not able to open the video. When I click on open now it just returns me back to the matlab page, which is understandable because I don't have a line of code that tells it to open the selected file. Is it possible to do that, have matlab open the selected file?
"Is it possible to do that, have matlab open the selected file?"
What does "open the selected file" mean exactly: open the file using its default application on your OS? Or open the file using some other non-default application on your OS? Or open the file as text within the MATLAB editor? Or do you want to actually import the file data using a suitable importing tool/function for that file format, so that you can perform further processing of the file data within MATLAB?
The more accurately you describe what you want, the easier it is for us to help you.
I want it to open the file using its default application
[filename,filepath] = uigetfile('*.mov');
if isempty(filename)
%user cancel, handle appropriately here
end
fullname = fullfile(filepath, filename);
open(fullname)
Using path as the name of a variable is not recommended as path controls the MATLAB search path.

Sign in to comment.

Answers (1)

Jan
Jan on 11 Apr 2021
Edited: Jan on 11 Apr 2021
Maybe you want to open a file inside this folder?
function main
myPath = fileparts(mfilename('fullpath')); % Folder of this M-file
Folder = fullfile(myPath, 'NameOfSubfolder'); % Subfolder inside
Data = load(fullfile(Folder, 'YourData.mat')); % A specific file there
...
end

Categories

Tags

Asked:

on 11 Apr 2021

Commented:

on 12 Apr 2021

Community Treasure Hunt

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

Start Hunting!