I have a question for the reading of multiple files .txt in the same times and same script. I have a principal folder Matlab in which there are 7 subfolders Folder1 to Folder 7 in which of them there is un document file.txt.
I would like to read each of 'file.txt' in a script that I run in the curent folder Matlab. is there a fast way to do this? Or am I forced to do load file.txt for each folder.
You can use dir to list all your Folder. Then you can create the path of your file for each folder and load this file.
folder = dir('Folder*'); %list all the folder whose name start with 'Folder'
for ii = 1:length(folder)
s{ii} = fullfile(folder(ii).name,'text.txt'); %create the path for each file
load(s{ii});
end
In a for loop you can create the folder name:
for i = 1:n
name = ['folder',int2str(i)]
% then you can open and read the file
fileID = fopen([name,'\file.txt'])
data = fread(fileID)
% Don't forget to close the file
fclose(fileID)
end
Related
How can I allow user to add additional dependenies after the source code was compiled with mcc.
I was thinking about an empty folder next to the executable, where the users can add the needed .mat-files, but I can't add the folder path to my executable (since addpath is not allowed in deployed applications).
Any ideas?
This answer assumes that your code can be customised by data contained in one or more .mat files at runtime.
You can point your code to look at a folder where the optional .mat file(s) would be located.
For example in the users home folder with a sub folder being the name of your application (or in the local app data) or whereever...
If you want it in a sub folder where the exe is, you can do this as well, you find the exe path using (on windows):
[status, result] = system('path');
installpath = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'));
fprintf ( 'The exe install path is "%s"\n', installpath );
Then your code looks to load for example:
file2load = fullfile ( installpath, 'subFolder', 'runtimeCustomisation.mat' )
if exist ( file2load, 'file' ) == 2
"doSomething with the file"
end
Or something like that.
Recall that this is for dependencies which are .mat files only.
Below is the code I am working currently:
cd '/Users/K3iTH/Desktop/Ubuntu (Shared)/Trial/'
filename = 'slice_quarter0';
k = 1
inputfile = sprintf('%s.%d.csv','filename',k-1);
data = csvread(inputfile,1,0);
I am trying to load the .csv file exported in Ubuntu. The code is run until inputfile. When I try to run data = csvread(inputfile,1,0);, it stated file not found.
Any mistake I did?
If the file slice_quarter0.csv exists, and is located in your working directory then you should modify your sprintf call to
inputfile = sprintf('%s%d.csv',filename,k-1);
Otherwise, csvread will try to open a file named filename.0.csv, which will cause the error you have since it presumably does not exist.
I have a simple script in matlab and I want to load a file. It seems it only works if the file is in the same dir as the script. If I add the file to a directory it does not read it.
For example:
fileID = fopen('myfile','r' ,'n', 'US-ASCII');
but when I put myfile in files:
fileID = fopen('files/myfile','r' ,'n', 'US-ASCII');
or
fileID = fopen('./files/myfile','r' ,'n', 'US-ASCII');
I get a -1 as a fileID. File cannot be read.
As per the comments, this is happening because you most likely added the path of where the script was located to your MATLAB path but you did not add the subdirectory where the file was in to your path. This is why it can't find the file. Therefore to avoid this in the future, you need to physically change the directory (i.e. the Working Directory) of where MATLAB is currently operating to where your script is stored.
It is then where local referencing should work. You can do this by either using the cd function, going to the top of your MATLAB window where you see the directory listing, clicking on the arrow to the right and pulling a drop down menu to change the directory, typing the actual directory you want by clicking on any blank space in the directory listing to enable a text box:
... or if you are running the code in the MATLAB editor, it'll request that you change directories as the script you are trying to run is not currently located in the working directory.
You can also programmatically add the subfolders in your script directory using mfilename, fileparts, genpath and addpath:
[dir, ~, ~] = fileparts(mfilename('fullpath')); % locate your script directory
addpath(genpath(fullfile(dir))); % add the folder and all subfolders to Matlab search directory
% then load your file.
fileID = fopen('myfile','r' ,'n', 'US-ASCII')
If it is also important that all outputs should be placed within the same directory as your script file, you can cd to your script directory:
cd(dir)
How can I manage to copy the files from multiple directories into one target directory IN MATLAB for example if the directories are organized as follow:
directory1
sub-directory1
sub-sub-directory1-1
file1
file2
sub-sub-directory1-2
file4
thisis-my-file
sub-directory2
sub-sub-directory2-1
file
myfile
sub-sub-directory2-2
file-case1
the result should be something like this:
target-directory
file1
file2
file4
thisis-my-file
file
myfile
file-case1
here is one of many possible solutions:
First, get all subfolders of your directory1:
% define the destination folder
destinationFolder = 'c:\temp';
% genpath delivers all subfolders of the given directory
directories = genpath('C:\directory1');
% the following regular expression gets all these subfolder, seperated by ';'
directories = regexp([directories ';'],'(.*?);','tokens');
Now you can use the dir-function inside a for-loop for getting all the files in the subfolders:
for i=1:length(directories)
% you could use a wildcard, if you only want some
% specific files to be moved in to the target directory.
% (filesep is a built-in function!)
files = dir([directories{i}{1} filesep '*.*']);
% use a second loop for copying your files
for j=1:length(files)
% build the path and copy the file to the desired destination
copyfile([directories{i}{1} filesep files(j).name)], destinationFolder);
end;
end;
you can use a matlab command for copying files which is copyfile('source','destination') like
copyfile('directory1/sub-directory1/sub-sub-directory1-1/file1.txt','target-directory')
copyfile('directory1/sub-directory1/sub-sub-directory1-1/file2.txt','target-directory')
copyfile('directory1/sub-directory1/sub-sub-directory1-2/file4.txt','target-directory')
copyfile('directory1/sub-directory1/sub-sub-directory1-2/thisis-my-file.txt','target-directory')
copyfile('directory1/sub-directory2/sub-sub-directory2-1/file.txt','target-directory')
like this way for all of your file and you get those file in your destination folder. you can also give a complete path of the each source and destination also.
file1 = fullfile(pwd,'folder','toto.txt');
file2 = fullfile(pwd,'folder','toto2.txt.sav');
movefile(file1,file2)
=>this creates a folder named toto2.txt.sav
whereas I'm looking for renaming the file in the same directory
any idea ?
Check the documentation:
Renaming a File in the Current Folder
In the current folder, rename myfunction.m to oldfunction.m:
movefile('myfunction.m','oldfunction.m')
Thus:
o=pwd
cd('folder')
movefile('toto.txt','toto2.txt.sav')
cd(o);
Was looking for this as well. Now on a windows machine if I want to rename all files in a folder, I do:
system('rename *.* *.sav')