I'm recording a sound and using wavwrite to save the wav file, but I need to save it in a specific folder in C:, such as in c:\Users\soundwav.
Here is an excerpt from my wavwrite code:
data(:,s)=getdata(ai,44100);
y = [y; data]
format shortg
c = clock;
fix(c);
a=num2str(c);
year=strcat(a(1),a(2),a(3),a(4),a(5));
month=strcat(a(19),a(20));
day=strcat(a(34),a(35));
hour=strcat(a(48),a(49));
min=strcat(a(63),a(64));
sec=strcat(a(74),a(75));
name=strcat(year,'-',month,'-',day,'-',hour,'-',min,'-',sec);
wavwrite(data,name);
You need to cd the path while saving it. I have included a line that concatenates the full path with name variable and then saves it.
data(:,s)=getdata(ai,44100);
y = [y; data]
format shortg
c = clock;
fix(c);
a=num2str(c);
year=strcat(a(1),a(2),a(3),a(4),a(5));
month=strcat(a(19),a(20));
day=strcat(a(34),a(35));
hour=strcat(a(48),a(49));
min=strcat(a(63),a(64));
sec=strcat(a(74),a(75));
name=strcat(year,'-',month,'-',day,'-',hour,'-',min,'-',sec);
name = strcat('c:\Users\soundwav\', name);
wavwrite(data,name);
To select the folder in which to save your file, you can use uigetdir which allows you to select the folder; then you can add to it the filename you've built.
directoryname = uigetdir
You can also specify a starting folder
directoryname = uigetdir('c:\user\')
name = strcat(directoryname, '\', name);
Hope this helps.
Related
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 program that loads data from a .txt file and performs some curve fitting. The input file name for this example is experiment09.txt.
After processing I want to save a variable with the same input filename but appended with something like _fit. So my saved workspace variable in this case would be experiment09_fit.txt.
I have gotten this far in MATLAB:
buf = length(filename)
saveName = filename(1:buf-7)
which gives me a saveName of experiment09 but I am at a loss as to how to add my chosen string on the end to make it experiment09_fit. Once I have a valid save name I will then just call
save(saveName, 'fittedValue', '-ASCII');
Help would be greatly appreciated.
What about this:
filename = 'experiment09.txt';
[pathstr, basename, ext] = fileparts(filename);
outname = [basename, '_fit', ext]; % will give 'experiment09_fit.txt'
Also use string concatenation for adding additional names to string variables.
For example,
filename = 'experiment09.txt';
[pathstr, name, ext] = fileparts(filename);
outputName1 = strcat(name,'_fit.');
outputName = strcat(outputName1,ext);
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...
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-
What is the best way to figure out the size of a file using MATLAB? The first thought that comes to mind is size(fread(fid)).
Please see the dir function as stated above.
Please note that the dir function works on files and not on directories only.
>> s = dir('c:\try.c')
s =
name: 'try.c'
date: '01-Feb-2008 10:45:43'
bytes: 20
isdir: 0
datenum: 7.3344e+005
You can use the DIR function to get directory information, which includes the sizes of the files in that directory. For example:
dirInfo = dir(dirName); %# Where dirName is the directory name where the
%# file is located
index = strcmp({dirInfo.name},fileName); %# Where fileName is the name of
%# the file.
fileSize = dirInfo(index).bytes; %# The size of the file, in bytes
Or, since you are looking for only one file, you can do what Elazar said and just pass an absolute or relative path to your file to DIR:
fileInfo = dir('I:\kpe\matlab\temp.m');
fileSize = fileInfo.bytes;
Use the fact that MatLab has access to Java Objects:
myFile = java.io.File('filename_here')
flen = length(myFile)
If you don't want to hardcode in your directory, you can use the built in pwd tool to find the current directory and then add your file name to it. See example below:
FileInfo = dir([pwd,'\tempfile.dat'])
FileSize = FileInfo.bytes
The question seems to indicate that fopen/fread/.. is used. In this case, why not seeking to the end of the file and reading the position?
Example:
function file_length = get_file_length(fid)
% extracts file length in bytes from a file opened by fopen
% fid is file handle returned from fopen
% store current seek
current_seek = ftell(fid);
% move to end
fseek(fid, 0, 1);
% read end position
file_length = ftell(fid);
% move to previous position
fseek(fid, current_seek, -1);
end
Matlab could have provided a shortcut..
More on ftell can be found here.
This code works for any file and directory (no need for absolute path) :
dirInfo=dir(pwd);
index = strcmp({dirInfo.name},[filename, '.ext']); % change the ext to proper extension
fileSize = dirInfo(index).bytes
Easy way to find size of file is:
enter these cammands
K=imfinfo('filename.formate');
size_of_file=K.FileSize
and get size of file.