I have an m file which creates an excel file using xlswrite. If I do not specify the the folder for the xls to be output to, the default is the users folder. I would prefer that it goes to the same folder as the m file. Is there a way to generalize this without explicitly entering the m file location? I plan on distributing this m file and not everyone will keep their m files in the same folder.
To get the path where running m-file is stored, check out:
help mfilename
help fileparts
You could simply use the current working folder as the save location. This is whatever the "current directory" is in the matlab window.
You can access it using the command pwd. This returns a string that is the path to the directory.
Then append \filename.ext to it and you'll be good to go!
Related
Is it possible to list all of the files in a directory of a certain file type, and then save them in the same directory but as a separate file type using MatLab?
In my case, I have 144 files saved as in a .fig format, but I would like to copy them as a .tif format so that I don't have to go and change each file manually.
I know I can list all the files in my directory using the dir function and I guess I could simply run for loop with i=1:length(dir) but I don't know how to isolate the files of a specific file type. I don't see filetype as a field name on the mathworks website for dir.
Thanks for any suggestions.
To list only files of type .fig:
files = dir('*.fig');
You can then loop over the names:
for k = 1:numel(files)
filename = files(k).name;
% do something with filename
end
I have a lot of folders each containing a lot of .txt files which i need to process with matlab.
i have a script finished which does it, it reads all .txt files in 1 folder and does some computing.
so if i want to process those files i move the script into the desired folder and run it, it is kinda lame to move the script every time.
is it possile to start a script in a standard directory, then make the script to ask me to browse for the desired directory to run the script in that directory? because it doesnt write any files, it just reads. after it finishes it should reset it, so i could browse to different folders every time. like save the path in a value and clear it at the end...
i use "fopen" in that script to open the .txt files, so it could be possible to assign the full path to that function, but its important to me to being able to browse the right folder every time i run the script.
i use
path=uigetdir;
fileid=fopen([path filesep 'file.txt']);
and it works just the way i wanted.
thanks #excaza and #Laure for a quick reply
I have a program that reads data from .trc files. But as of now, it can only read the .trc files that are in the current directory i.e the MATLAB folder. If I want it to read a file, I have to copy the file in MATLAB folder. Is there a way I can move to different directories and choose another .trc file? I have tried using dir, uigetdir etc. but nothing seems to work!
It's pretty straightforward to filter results with the dir command with a file extension if you specify the folder to search with a wildcard character in place of the file name. You can then loop over the generated list of file names in the output structure (as shown) or whatever you need to do with those files.
folderName = 'C:\Path\To\Target\Folder\';
fileList = dir(strcat(folderName, '*.trc'));
for k = 1:length(fileList)
fileHere = fullfile(folderName, fileList(k).name);
% Do what you need with the files in here
end
I need to read a group of dat files so when I do this it is working all right.
list_of_files=dir(fullfile('/home/username/Desktop/Old/MTP/Generate/schemes/o33smnpimp/data/', '*.dat'));
The thing is I want to do this for number of schemes (like o33smnpimp) where every scheme folder has a data folder so I tried something like this but it's not working. What could be the problem?
list_of_files=dir(fullfile('../data/', '*.dat'));
My matlab file lies in o33smnpimp folder.
.. indicates the parent directory, . the current directory. your code looks in /home/username/Desktop/Old/MTP/Generate/schemes/ for the sub directory data, assuming your working directory is /home/username/Desktop/Old/MTP/Generate/schemes/o33smnpimp.
Use
list_of_files=dir(fullfile('./data/', '*.dat'));
or
list_of_files=dir(fullfile('data', '*.dat'));
I start with Matlab and would like to know how could I access to a folder and get contents to access files and read them.
I have a variable in workspace tmpfolder that is equal to 'path to folder' but I don't find how could I make dir(tmpfolder) and get files, browse any file content to get a string value...
I would start with dir() and fopen().
More generally, try starting at the beginning: Working with Files and Folders.
If you have an image file in jpeg format in another folder named myimage and a text file called mytext, use:
prefix_image='myimage';
prefix_data='mytext';
fileformat='.jpg';
dataformat='.txt';
folder='C:\Users\khaled\Documents\MATLAB\';
image = imread(strcat(folder,prefix_image,fileformat));
data=textread(strcat(folder,prefix_data,fileformat),'%f');