I've
resDir = C:\temp\source\
--------\folder1
--------\folder2
--------\file.txt
%list the content of resDir
list = ls(resDir);
and I want to check that resDir contains folder1 and folder2 and that they are not empty
is there an equivalent of contains(java) or exist function ?
thanks
Use EXIST function to detect if a particular folder exists.
Function DIR returns structure array of all objects in the directory. Empty folder will contain only 2 objects: . (current directory) and .. (root directory).
resDir = 'C:\temp\source\';
folder = 'folder1';
folderfull = fullfile(resDir,folder); %# full path to the folder
if exist(folderfull,'dir')
foldercontent = dir(folderfull);
if numel(foldercontent) > 2
%# folder exists and is not empty
end
end
I don't think that there is a built-in equivalent of the Java function you refer to, but Matlab provides all the basics you need to write your own without too much difficulty. Hit the documentation for isdir, fileparts, etc.
Related
In my Scala code I want to create a folder "c:/temp" and then create a file "file.txt" within that folder. I don't want to have to use "c:/temp/file.txt". So, I want to use the relative path of the file to create it within that folder.
Imagine how a human creates a folder and then a file? He creates a folder; goes in the folder, and then creates the file inside that folder. That's what I want to do.
=====
Added the following to make this more clear:
Let's say I created the folder and I have a File object called myFolder that represents that folder. What I want is to be able to do something like myFolder.createFile("file.txt").
val subFile = new File(myFolder, "file.txt")
From the description of the File(File parent, String child) constructor found at the docs page:
Creates a new File instance from a parent abstract pathname and a child pathname string.
I was assigned a matlab assignment where I was given 25000 pictures of cats and dogs all stored in one folder. My question is how can I use the imagedatastore function on matlab to store these files into one single variable containing two labels (cats and dogs). Each image stored in the file follow the following format:
cat.1.png,
cat.2.png,
.....,
cat.N.png,
dog.1.png,
dog.2.png,
.....,
dog.N.png,
Ideally I think labeling them based on image name would probably be the best approach to this. How ever I've tired doing this using various implementations methods but I keep failing. Any advice on this would be greatly appreciated!
The steps for both image data stores are the same:
Find all the image files with a matching name with dir.
Rebuild the full path to these files with fullfile.
Create the image data store with the files.
My code assumes that you are running the script in the same folder in which images are located. Here is the code:
cats = dir('cat.*.png');
files_cats = fullfile({cats.folder}.', {cats.name}.');
imds_cats = imageDatastore(files_cats);
dogs = dir('dog.*.png');
files_dogs = fullfile({dogs.folder}.', {dogs.name}.');
imds_dogs = imageDatastore(files_dogs);
You could also use the short path:
imds_cats = imageDatastore('cat.*.png');
imds_dogs = imageDatastore('dog.*.png');
If you want to use a single image data store and split files into categories within it (without using folder names, since all your files seem to be located in the same directory):
cats = dir('cat.*.png');
cats_labs = repmat({'Cat'},numel(cats),1);
dogs = dir('dog.*.png');
dogs_labs = repmat({'Dog'},numel(dogs),1);
labs = [cats_labs; dogs_labs];
imds = imageDatastore({'cat.*.png' 'dog.*.png'},'Labels',labs);
My AppFolder contains two sub-directories: Folder1, Folder2.
Each of the folders (Folder1, Folder2) has a bunch of files inside. I wanted to get a list of the files inside each of the sub-directories.
How do I check if Folder 1 for example contains files inside it?
Use like below:
result = client.files().listFolder("/Folder1");
System.out.println(result.getEntries().size()); // Should be != 0
The documentation is here :
https://dropbox.github.io/dropbox-sdk-java/api-docs/v2.0.x/com/dropbox/core/v2/files/FileMetadata.html
Hi and I am little bit new in matlab.
I have two different folders in my laptop, each one contains about 400 different files. I want to load all these files (400 from first folder and 400 from second folder), I tried like that but doesn't work:
folder1=('E:\results\1\');
folder2=('E:\results\2\');
data=load([folder1,'*.m']);
data1=load([folder2,'*.m']);
and then I want to take first file from folder1 and subtracted from first file from folder1 and save it in new folder. and do that for all other files ... etc
can some expert give me any suggerstion!!
thanks in advance.
Pretty sure load takes one file at a time. Try a simple variant like this:
folder1=('E:\results\1\');
folder2=('E:\results\2\');
files1 = dir( [folder1,'*.m'] );
files2 = dir( [folder2,'*.m'] );
data = cell(length(files1),1); % I don't know what's in the mat files, but let's start with a cell array
data1 = cell(length(files2),1);
for ii=1:length(files1)
data{ii} = load(fullfile(folder1,files1(ii).name));
end
for ii=1:length(files2)
data1{ii} = load(fullfile(folder2,files2(ii).name));
end
There are other, more one liner ways, but this is a fairly pedantic.
Been trying (unsucessfully) to use 'which' to locate a .m file inside a package. For example, calling "which('Company.m')", when Company.m is inside a +Contents folder.
So if my current folder is C:\Users\Documents\Contents (path added to Matlab paths), "which('Company.m')" indicates no file found, but if my current folder is C:\Users***\Documents\Contents\ +Contents, then it will know the location.
Why is this? I thought that the 'which' command recursively searches through all subdirectories? Is there anyway to retrieve the path name of 'Company.m' without having to specifically source into that folder?
That should be:
which Contents.Company
If you dont know beforehand in which package it resides (or if its even in one), you could import them all:
import Contents.*
import OtherPackage.*
which -all Company
If you are still not satisfied, you could get a list of all top-level packages available, and search the methods they expose for the function you want:
%# warning: this might take more than a few seconds
p = meta.package.getAllPackages;
b = cellfun(#(pkg) ismember('Company',{pkg.FunctionList.Name}), p);
idx = find(b, 1, 'first');
p{idx}.Name