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.
Related
I'm trying to build a Machine Learning - Image Recognition using Create ML in Xcode 10.1 Playground but I'm having some problems to put my data in the model.
I have a folder with images numbered from 1 to 1336 and a .csv file with 2 columns (the image name and the image classification).
I don't know exactly how to put this in the model.
I have this until now:
import Cocoa
import CreateML
let data = try MLDataTable(contentsOf: URL(fileURLWithPath: "/Users/x/Desktop/CoreML/project/file.csv"))
let(trainingData, testingData) = data.randomSplit(by: 0.8, seed: 1)
let Classifier = try MLImageClassifier *need help here*
let evaluationMetrics = sentimentClassifier.evaluation(on: testingData)
let evaluationAccuracy = (1 - evaluationMetrics.classificationError) * 100
let metaData = MLModelMetadata(author: "x", shortDescription: "Model", version: "1.0")
try classifier.write(to: URL(fileURLWithPath: "/Users/x/Desktop/CoreML/project/XClassifier.mlmodel"))
I believe it is not possible to feed labels to MLImageClassifier via .csv or any other separate file. You have only two options: use file names as labels or use directories as labels (probably preferable in your case of many images):
let model = try MLImageClassifier(trainingData: .labeledDirectories(at: trainingDir))
let evaluation = model.evaluation(on: .labeledDirectories(at: testingDir))
You will need to put images into subdirectories named as labels in your .csv file.
I was just struggling with this myself. Here is a solution to re-organise data for CreateML. All credit goes to Tony T1 who came up with this script.
Place images and CSV file into a single folder.
In Automator, create a new workflow like this:
Run the workflow. Select your CSV and watch the images get sorted into their respective folders!
The script is as follows:
cd "${1%/*}"
while read line
do
FolderName=${line%;*}
ImageName=${line#*;}
mkdir "$FolderName"
mv "$ImageName" "$FolderName"
done < "$1"
DF20 is the starting folder, you can change that to whatever you wish
my CSV was separated by ";". If your CSV is separated by ",", change that symbol in the script (e.g. FolderName=${line%,*} )
In my CSV, classes were columnA and images columnB. Switch this around depending on your case.
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);
I have a folder decawaveLogs that has three subfolders of data files (.txt) - longrange, shortrange and moving. SO I did the following to access each subfolder data at a time
rangingType='longrange';
logsFolder = '/Users/X/Google Drive/rangingAccuracy\decawaveLogs'; %for Mac
decawaveFiles = dir(fullfile(logsFolder,rangingType,'*.txt'));
When I run the complete program, I get en empty struct for decawaveFiles
You are mixing forward and backward slashes in logFolder. That might be an issue.
I need to download a lots of data from Ftp server. Now I want to use matlab for this task
My ftp address is
http://e4ftl01.cr.usgs.gov/MOLT/MOD09GA.005/2008.03.17/
Filenames are:
"MOD09GA.A2008077.h23v05.005.2008080122921.hdf" "MOD09GA.A2008077.h22v05.005.2008080122814.hdf "
Data will get saved in a folder in drive E.
I want to download the file using Matlab. In this way I want to download data from the ftp server.
Waiting for your kind help.
Thanks in Advance
thank you for your code.but there is a problem.i just need 2data that are below: "MOD09GA.A2008077.h23v05.005.2008080122921.hdf" "MOD09GA.A2008077.h22v05.005.2008080122814.hdf "
but i need just this ID of that data, to eliminate the extra letters:
"MOD09GA.A2008077.h23v05.hdf"
"MOD09GA.A2008077.h22v05.hdf "
I want to have 2 data through 313 data. how can do that?
please help.
Technically, you're downloading using HTTP, not FTP.
You can have Matlab copy the contents of the URL to a local file:
url = 'http://e4ftl01.cr.usgs.gov/MOLT/MOD09GA.005/2008.03.17/MOD09GA.A2008077.h23v05.005.2008080122921.hdf'
urlwrite(url, 'tmp.hdf');
then to see what's in the file:
hdfinfo('tmp.hdf')
As dpwe already said, you are not technically downloading via FTP.
However here is a solution tailored to your case, which first gets all the filenames you want, namely the ".*hdf" files. It then just loops over all found *.hdf files and downloads them to 'localPath'.
This is definitely not the easiest or cleanest way to do this, but it works and it should fit your needs, I hope.
% URL you want to grab from - has to end with a /
URL = 'http://e4ftl01.cr.usgs.gov/MOLT/MOD09GA.005/2008.03.17/';
% Local path on your machine
localPath = 'E:/myfolder/';
% Read html contents and parse file names with ending *.hdf
urlContents = urlread(URL);
ret = regexp(urlContents, '"\S+.hdf"', 'match');
% Loop over all files and download them
for k=1:length(ret)
filename = ret{k}(2:end-1);
filepathOnline = strcat(URL, filename);
filepathLocal = fullfile(localPath, filename);
urlwrite(filepathOnline, filepathLocal);
end
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.