I have lots of text files with name file1,file2,file3,.... in a folder called 'text_files'. When I open manually that folder in Matlab directory and perform following it works fine.
textFiles = dir('*.txt');
for k = 1:length(textFiles);
filename = textFiles(k).name;
data = fopen(filename,'r');
datamatrix=textscan(data, '%f%f','CollectOutput',1);
data1 = datamatrix{:,1};
r=data1(:,1);v0=data1(:,2);
figure(k);
ph=plot(r,v0);
xlabel('a');
ylabel('b');
temp=['fig',num2str(k),'.eps'];
print(gcf,'-depsc',temp);
fclose(data);
end
The path to text files in my Mac is '/Users/ram/group1/sales/text_files'. What I want to do is instead of manually opening the folder in matlab directory, I want to write a script that does it automatically for me. So, I guess I have to make some change in
textFiles = dir('*.txt');
Any help will be much appreciated.
Use full path:
src_dir = '/Users/ram/group1/sales/text_files';
textFiles = dir( fullfile( src_dir, '*.txt' ) );
for k = 1:numel(textFiles)
filename = fullfile( src_dir, textFiles(k).name ); % NOTE the use of src_dir here as well!
% continue as usuall...
Related
I have a folder origin_training with subfolders like FM, folder1, folder2, ... . I can get the list of image files in .png format into a cell called FM.
FM_dir = fullfile(origin_training, 'FM');
FM = struct2cell(dir(fullfile(FM_dir, '*.png')))';
My goal is to match the names in my folder with the images in my cd, and make a new folder FM with the images from cd. Image names in the two paths are identical. I can do that with:
% mother_folder = '...' my current directory
filenames = FM(:,1);
destination = fullfile(mother_folder, 'FM', FM(:,1));
cellfun(#copyfile,filenames, destination);
This is really slow. Takes minutes even for small amounts of images (~30).
My current directory has ~ 10000 images (the ones that correspond to FM, folder2, folder3, ...). What am I missing here?
An alternative would be to create a shell command and execute it. Assuming FM contains full paths for each file, or relative paths from the current directory, then:
FM_dir = fullfile(origin_training, 'FM');
destination = fullfile(mother_folder, 'FM');
curdir = cd(FM_dir);
FM = dir('*.png');
cmd = ['cp ', sprintf('%s ',FM.name), destination];
system(cmd);
cd(curdir);
If you're on Windows, replace 'cp' by 'copy'.
Note that here we're not creating a shell command per file to be copied (presumably what copyfile does), but a single shell command that copies all files at once. We're also not specifying the name for each copied file, we specify the names of the files to be copied and where to copy them to.
Note also that for this to work, mother_folder must be an absolute path.
I somehow put my code into a function and now it works at expected speed. I suspected that cd had to do with the low speed so I am only passing the full directories as character vectors.
The same approach works for my purpose or with a slight modification to just copy from A to B. As now, it works to match files in A to those on B and copy to B/folder_name
function [out] = my_copyfiles(origin_dir, folder_name, dest_dir, extension)
new_dir = fullfile(origin_dir, folder_name);
% Get a cell with filenames in the origin_dir/folder_name
% Mind transposing to have them in the rows
NEW = struct2cell(dir(fullfile(new_dir, extension)))';
dest_folder = fullfile(dest_dir, folder_name);
% disp(dest_folder)
if ~exist(dest_folder, 'dir')
mkdir(dest_folder)
end
%% CAUTION, This is the step
% Use names from NEW to generate fullnames in dest_dir that would match
filenames = fullfile(dest_dir, NEW(:,1));
destination = fullfile(dest_folder, NEW(:,1));
% if you wanted from origin, instead run
% filenames = fullfile(new_dir, NEW(:,1));
% make the copies
cellfun(#copyfile,filenames, destination);
%Save the call
out.originDir = new_dir;
out.copyFrom = dest_dir;
out.to = dest_folder;
out.filenames = filenames;
out.destination = destination;
end
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 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!
I need to extract the first value of the following code:
3.43099,70.8539,91.701,FAIL
The file has the '.sol' extension, but it can be read in notepad or Matlab.
I just want to know how to to read in all *.sol files in a folder and how to write the extracted value in a text file.
Thanks a lot, i would be grateful.
NEW
ita='"';
for i=1:size(z,2)
word_to_replace=input('Replace? ','s');
tik=input('Replacement? ','s');
coluna=input('Column? ');
files = dir('*.txt');
for i = 1:numel(files)
if ~files(i).isdir % make sure it is not a directory
contents = fileread(files(i).name);
fh = fopen(files(i).name,'w');
val=num2str(z(i,coluna));
word_replacement=strcat(tik,val,ita);
contents = regexprep(contents,'word_to_replace','word_replacement');
fprintf(fh,contents); % write "replaced" string to file
fclose(fh) % close out file
end
end
end
Many thanks
File extension does not make a difference as to what MATLAB can "read", use the fileread command to load in the file and parse its contents. You can then split on commas, since it looks like it is comma separated
files = dir('*.sol');
fh = fopen('outFile.txt','w');
for i = 1:numel(files)
if ~files(i).isdir % make sure it is not a directory
contents = fileread(files(i).name);
parts = regexp(contents,',','Split');
fprintf(fh,[parts{1},'\n']);
end
end
fclose(fh)
This should do what you want. It will find all files in the current directory with the .sol extension, loop through all of them, grab the first value, and write it out to a text file.
Find and replace
Finding and replacing is relatively simple as well. You can do the same looping, read the file contents, run a replacement, and then rewrite that out to the same file.
files = dir('*.sol');
for i = 1:numel(files)
if ~files(i).isdir % make sure it is not a directory
contents = fileread(files(i).name);
fh = fopen(files(i).name,'w'); % open handle to same file just read for overwriting
contents = regexprep(contents,'toReplace','replacement'); % do string replacement
fprintf(fh,contents); % write "replaced" string to file
fclose(fh) % close out file
end
end
How can I process all the files with ".xyz" extension in a folder? The basic idea is that I want a list of file names and then a for loop to load each file.
As others have already mentioned, you should use the DIR function to list files in a directory.
If you are still looking, here is an example to show how to use the function:
dirName = 'C:\path\to\folder'; %# folder path
files = dir( fullfile(dirName,'*.xyz') ); %# list all *.xyz files
files = {files.name}'; %'# file names
data = cell(numel(files),1); %# store file contents
for i=1:numel(files)
fname = fullfile(dirName,files{i}); %# full path to file
data{i} = myLoadFunction(fname); %# load file
end
Of course, you would have to supply the function that actually reads and parses the XYZ files.
Use dir() to obtain a list of filenames. You can specify wildcards.
You could use
fileName=ls('*xyz').
fileName variable will have the list of all the filenames which you can use in the for loop
Here is my answer:
dirName = 'E:\My Matlab\5';
[sub,fls] = subdir(dirName);
D = [];
j = 1;
for i=1:length(sub),
files{i} = dir( fullfile(sub{i},'*.xyz') );
if length(files{i})==1
D(j) = i;
files_s{j} = sub{i};
j=j+1;
end
end
varaible files_s returns the desire paths that contain those specific data types!
The subdir function can be found at:
http://www.mathworks.com/matlabcentral/fileexchange/1492-subdir--new-