I have several subfolders in my parentFolder but I only want to read the .nc files from a few specific subfolders. I thought I had it figured out with this:
parentFolder = '/Users/AM/workspace/';
cd(parentFolder)
s1 = 'daytime/instant';
s2 = 'daytime/monthly';
s3 = 'nighttime/instant';
s4 = 'nightttime/monthly';
sub_f = {s1,s2,s3,s4};
s = strcat(parentFolder, sub_f);
% 1x4 cell with pathnames to files
filePattern = fullfile(s, '*.nc');
ncFiles1 = cell(length(filePattern),1);
for k = 1:4
ncFiles1{k} = dir(filePattern{k});
end
ncFiles = [ncFiles1{1}; ncFiles1{2}; ncFiles1{3}; ncFiles1{4}];
filename = cell(length(ncFiles),1);
for k = 1:length(ncFiles)
filename{k} = ncFiles(k).name;
end
I ended up with a 131x1 cell array of all the filenames I wanted, but when I tried to read the data I got this error message:
Error using internal.matlab.imagesci.nc/openToRead (line 1259)
Could not open instant_SR_CR_DR_2008-07-01T00-15-56ZD_day_CFMIP2_2.90.nc for reading.
I know the files are okay because I can read them in by going to each subfolder individually. Is there a better way to read files out of specific subfolders? Any reason I'm getting that error when I try to open the files?
Thanks in advance!
Related
I have a folder with 10 subfolder each has about 100 different files including text files and different extension images. I just need to copy the image files with JPG extension and move it to another single folder.
I am using this code:
clear all
clc
M_dir = 'X:\Datasets to be splitted\Action3\Action3\'% source directory
D_dir = 'X:\Datasets to be splitted\Action3\Depth\'
files = dir(M_dir);% main directory
dirFlags = [files.isdir];
subFolders = files(dirFlags);%list of folders
for k = 1 :length(subFolders)
if any(isletter(subFolders(k).name))
c_dtry = strcat(M_dir,subFolders(k).name)
fileList = getAllFiles(c_dtry)%list of files in subfolder
for n1 = 1:length(fileList)
[pathstr,name,ext] = fileparts(fileList{n1})% file type
%s = dir(fileList{n1});
%S = s.bytes/1000;%file size
Im = imread(fileList{n1});
%[h,w,d] = size(Im);%height width and dimension
if strcmp(ext,'.jpg')|strcmp(ext,'.JPG')%)&S>=50&(write image dimension condition))% here you need to modify
baseFileName = strcat(name,ext);
fullFileName = fullfile(D_dir, baseFileName); % No need to worry about slashes now!
imwrite(Im, fullFileName);
else
end
end
end
end
But the code is stopped with an error when a text file being processed.
The error says:
Error using imread>get_format_info (line 491)
Unable to determine the file format.
Error in imread (line 354)
fmt_s = get_format_info(fullname);
Error in ReadFromSubFolder (line 16)
Im = imread(fileList{n1});
Thanks
Your code is reading the data before you check the extension
Im = imread(fileList{n1});
is before
if strcmp(ext,'.jpg')|strcmp(ext,'.JPG')
edit
For finding files which end 'vis' you can usestrcmp
strcmp ( name(end-2:end), 'vis' )
For completeness you should also check that name is longer than 3 char.
Here is a relatively simpler solution using dir and movefile or copyfile depending on whether you want to move or copy:
%searching jpg files in all subdirectories of 'X:\Datasets to be splitted\Action3\Action3'
file = dir('X:\Datasets to be splitted\Action3\Action3\**\*.jpg');
filenames_with_path = strcat({file.folder},'\',{file.name});
destination_dir = 'X:\Datasets to be splitted\Action3\Depth\';
%mkdir(destination_dir); %create the directory if it doesn't exist
for k=1:length(filenames_with_path)
movefile(filenames_with_path{k}, destination_dir, 'f'); %moving the files
%or if you want to copy then: copyfile(filenames_with_path{k}, destination_dir, 'f');
end
I need to check whether a wav file in Matlab work folder exists ou not. If it does, I need to load the file into a variable (file in my case), i use this code but it doesn't work.
if strcmp(file,'\n')==0
file='test.wav';
elseif findstr(file,'.')==''
file=strcat(file,'.wav');
end
[TestWave,Fs] = audioread(file);
You don't say if you are trying to find a particular .WAV file, or just any .WAV file...
If you just want to know if a particular file (of any kind) exists, use the exist() function. It returns value 2 if a file exits:
myFileName = 'test.wav';
myDirectory = 'c:\temp';
filepath = fullfile(myFileName,myDirectory);
if exist(filepath,'file') == 2
[TestWave,Fs] = audioread(file);
end
Otherwise, just search for the files you need using dir():
myDirectory = 'c:\temp';
wildcard = '*.wav';
theseFiles = dir(fullfile(myDirectory,wildcard));
for i = 1:length(theseFiles)
thisFilePath = fullfile(myDirectory,theseFiles(i).name);
[TestWave,Fs] = audioread(thisFilePath); % Load this file
% Do something with the loaded file...
end
I have a folder containing 9 .htk files. I need to use "dir", and then "readhtk" in a loop to import them to MATLAB, but DIR appears to give 10 files instead of 9! here is my code:
htkfiles = dir('/Users/Desktop/Acsegment/mfcdir/*.htk');
nhtkfiles = length(htkfiles); % 10!!! It should be 9 tough!
data = cell(nhtkfiles,2);
for k = 1:nhtkfiles
b(k,1) = strcat({'/Users/Desktop/Acsegment/mfcdir/'},{htkfiles(k,1).name});
eval(['data{k,1} = readhtk(b{k,1});']);
end
When looking at the filenames in htkfiles, I have them like this:
htkfiles(1,1).name = '.htk'
htkfiles(2,1).name = 'fadg0_si1279.htk'
htkfiles(3,1).name = 'fadg0_si1909.htk'
htkfiles(4,1).name = 'fadg0_si649.htk'
htkfiles(5,1).name = 'fadg0_sx109.htk'
htkfiles(6,1).name = 'fadg0_sx19.htk'
htkfiles(7,1).name = 'fadg0_sx199.htk'
htkfiles(8,1).name = 'fadg0_sx289.htk'
htkfiles(9,1).name = 'fadg0_sx379.htk'
htkfiles(10,1).name = 'faks0_si943.htk'
Comparing to what I see in that folder, the first file is not supposed to be there! Anyone got any ideas why Im getting one extra file?
As mentioned in the comments: the dir command actually works properly, there just happens to be a hidden file.
These files starting with a dot could be removed from your list like so:
d=dir;
d(strncmp({d.name},'.',1))=[];
First time here so please be gentle
So the basic idea is i have folders with just txt files that has about 20000 points each. I only want specific intervals from each of them.
I have a made a single file with the ranges for that looks like this
. 2715 2955
1132 1372
each row representing the range i want in one file
I want to batch load all the files and export the just the ranges of each. Ive lost too much sleep over this please help
dirName = '*'; %# folder path
files = dir( fullfile(dirName,'*.txt') ); %# list all *.xyz files
files = {files.name}' ; %'# file names
data = cell(numel(files),1) ; %# store file contents
for u=1:numel(files)
A=files{u} ; %# full path to file
files{u};
STR1 = A
B=load(STR1);
end
This is all i have come up with in 2 days. im new to matlab
Thanks
A very good help is the matlab help of fscanf, http://www.mathworks.co.uk/help/matlab/ref/fscanf.html. Also, in your load you don't have the path. Replace the last two lines in your for loop with:
STR1 = [dirName A]
fileID = fopen(STR1,'r');
formatSpec = '%f';
B = fscanf(fileID,formatSpec)
Or try:
delim = ' ';
nrhdr = 0;
STR1 = [dirName A]
A = importdata(STR1, delim, nrhdr);
A.data will be your data, I'm assuming no header lines.
I have a directory that contains 200 jpeg images. What I want is to rename all these images. So how can I rename all my images at the same time. for example I want to rename the first image to "hello1", "hello2" for the second, "hello3" for the third....>"hello200" for the 200.
Below you can find my code:
maximagesperdir = inf;
directory='imagess';
dnames = {directory};
fprintf('Reading images...');
cI = cell(1,1);
c{1} = dir(dnames{1});
if length(c{1})>0,
if c{1}(1).name == '.',
c{1} = c{1}(4:end);
end
end
if length(c{1})>maximagesperdir,
c{1} = c{1}(1:maximagesperdir);
end
cI{1} = cell(length(c{1}),1);
for j = 1:length(c{1}),
cI{1}{j} = double(imread([dnames{1} '/' c{1}(j).name]))./255;
end
fprintf('done.\n');
Here is some code to rename all the files in the current directory, the code you showed appears to read and not rename.
fnames = dir('*.jpg');
for i = 1:length(fnames)
old_name = fnames(i).name;
new_name = sprintf('hello%d.jpg', i);
movefile(old_name, new_name)
end
If you're just looking to rename the files and not to perform an operation on the images and then rename, there's always the total commander program which is a useful little thing to have. You select all the files and by using ctrl+m you select the way by which you would like to rename them (date, name, etc). Very simple if you're looking to perform the renaming operation scarcely. I'm just saying...