How to read fetch wav file one by one in matlab? - string

this is my Matlab code, I want to read .wav file from the same file or another file.
str=['1.wav';'2.wav';'3.wav';'4.wav';'5.wav';];
for i=1:5
[y, fs]=wavread(str(i));
a = miraudio(str(i));
z = mirzerocross(a)
close all
end
it gives me error like..
Error using TRYFINAL (line 1)
Error using vertcat
Dimensions of matrices being concatenated are not consistent.

Your OP is failing because of how MATLAB character arrays are implemented (#patrik has a very good explanation in this recent question). If you want to use a character array every row must be the same length, requiring you to pad the entries somehow which, while doable, isn't very efficient. The alternative is to use cell arrays, as #nkjt suggested, which will work for the implementation outlined in your OP.
A more general approach, however, is to use the data structure returned by MATLAB's dir command to identify all of the *.wav files in a directory and perform some operation on all of them.
pathname = 'C:\somewavfiles'; % Full path to a folder containing some wav files
wavfiles = dir(fullfile(pathname, '*.wav')); % Obtain a list of *.wav files
% Loop over all the files and perform some operations
for ii = 1:length(wavfiles)
filepath = fullfile(pathname, wavfiles(ii).name); % Generate the full path to the file using the filename and the pathname specified earlier
[y, fs] = wavread(filepath);
a = miraudio(filepath);
z = mirzerocross(a);
end
I have used fullfile in a few places rather than concatenating strings with a slash in order to avoid compatibility issues between operating systems. Some use \ and others use /.
Also note that, as explained by the documentation, you can use wildcards (*) in dir calls to narrow down the list of files returned.

Related

removing the trailing extension from a string in Python3

I have the python script below to iterate over all files ending with 'mkv', and print the same string without the 'mkv' at the end.
But, instead it prints the original filename including the 'mkv', why??
files=os.system('find /media/radamand/230_GB -name *mkv')
for file in str(files):
converted_filename=file[0:-3]
print(converted_filename)
Your os.system call executes your find command, sends its output to your interpreter standard output stream (which is why you're seeing your matching files including the "mkv" at the end, as this output is not the result of your print function in your later code), and then simply returns the exit code.
So your files variable actually gets an assignment of the integer 0.
Your for loop then casts files from an int into a string ('0') and thus your for loop now actually means: "loop through each character of the string files" (there is only one however), which, in this case, due to your slicing of [:-3] on a string of only one character, evaluates as an empty string which gets passed to your print function.
So, os.system isn't designed for what you are trying to achieve.
If you potentially have other folders in the parent folder you are searching, that may also have the filenames you are looking for, then I would recommend using the glob module.
import glob
files = glob.glob("/media/radamand/230_GB/*mkv") # Returns a list of strings for matched files
for file in files:
print(file[:-3])
You can add and set the keyword arguments recursive and/or include_hidden to True if required.
If, however, you are only looking for the files in the current folder, you can use fnmatch:
import fnmatch
import os
for file in os.listdir("/media/radamand/230_GB"):
if fnmatch.fnmatch(file, "*mkv"):
print(file[:-3])

Is there a way to compare the format in which a line of a text file was written in Fortran?

I'm developing a Fortran program that must obtain some data from a text file and generate another text file using specific data from the first one.
The input file have many lines written in several specific formats which I know of. Although I know the formats, the lines in this file are generated in a "random way".
It would be much easier to generate the output file if I could compare the format in which each line was written, then I would know exactly what data I can get from that line of the input file to use it in the output file.
What I need is something like, for example, knowing that the format of the line read and stored in the LINHA variable is described in the FORMATO variable, do something like:
    
IF (FORMATO = '(1X, 15,3F8.1,2 (5A, 1X))') THEN
READ (LINHA, '(6X, F8.1)') my_variable
END IF
Because there might be another format such as
'(6A, 2F8.1, F8.6,2 (6A))'
in which, if I use the same READ statement, I will read an F8.1 variable in my_variable, however this value is not the correct one.
A (not so elegant) work-around that I can think of is to read the entire line using the advance = no option of read() and parse each character in the line separately. While doing so, you may count white spaces or other specific characters that you know of and then identify the different formats from there.
It would be helpful if you could give more specifications of the nature of the task.
The best option is to read without format, keeping each line in a character array. Then read the line variable as an internal file with the required format using the variable IOSTAT in order to check if the format is the correct.
INT max_size = 80
CHARACTER(LEN=max_size) :: line
READ(*,*) line
READ(line,'(1X, 15,3F8.1,2 (5A, 1X))',IOSTAT=ios) var1, var2, ...
Problem solved using a mixture of some of the suggestions posted.
I read each of the lines of the input file in an internal variable (RLINFILE) in the format '(A165)'. After that, I read all the contents of the string that I put in this internal variable in several dummy variables, using the format I knew of the lines from where I wanted to get some information (read all the information of the line in the desired and get IOSTAT = 0 guarantee that this is the correct line), so if the result of the reading is ok (IOSTAT = 0), it is because the line I just read was the correct one for the information I wanted, so I store the contents of some of the dummy variables that represent the values that interest me. In the code, the solution looked something like this:
OPEN(UNIT=LU1,FILE=RlinName,STATUS='OLD')
ilin = 0
formato = '(14X,A,1X,F7.1,1X,F7.1,5X,A,1X,A,1X,A,5X,A,I5,1X,A,I3,3F8.1,A,A,A,1X,A,2(1X,F8.2),1X,A,1X,A)'
DO WHILE (.TRUE.)
READ(LU1,'(A165)',END=300) RLINFILE
READ(RLINFILE,formato,IOSTAT=linhaok) dum2_a1,dum2_f1,dum2_f2,dum2_a2,dum2_a3,dum2_a4,dum2_a5,dum2_i1,dum2_a6,dum2_i2,dum2_f3,dum2_f4,dum2_f5,dum2_a7,dum2_a8,dum2_a9,dum2_a10,dum2_f6,dum2_f7,dum2_a11,dum2_a12
IF(linhaok.EQ.0) THEN
ilin = ilin+1
rlin_lshu(ilin) = dum2_a4
rlin_nbpa(ilin) = dum2_i1
rlin_ncir(ilin) = dum2_i2
rlin_ppij(ilin) = dum2_f3
rlin_pqij(ilin) = dum2_f4
rlin_tapn(ilin) = dum2_a7
END IF
END DO
300 CLOSE(UNIT=LU1)
The description of the problem you are trying to solve is a bit vague to me, but the simplest solutions that comes to my mind, given the description of the problem, is to modify the original code that generates the input data file, to write the used Fortran READ format before the data line in the input file. This way, you can read the format as a string and use it in the subsequent data IO in your second code.
If you describe the specific task your tryting to accomplish in more details, perhaps more experienced Fortranners could help.

substitue string by index without using regular expressions

It should be very easy, but I am looking for an efficient way to perform it.
I know that I could split the string into two parts and insert the new value, but I have tried to substitute each line between the indexes 22-26 as follows:
line.replace(line[22:26],new_value)
The Problem
However, that function substitutes everything in the line that is similar to the pattern in line[22:26].
In the example below, I want to replace the marked number 1 with number 17:
Here are the results. Note the replacement of 1 with 17 in several places:
Thus I don't understand the behavior of replace command. Is there a simple explanation of what I'm doing wrong?
Why I don't want RE
The values between index 22-26 are not unified in form.
Note: I am using python 3.5 on Unix/Linux machines.
str.replace replaces 1 sub-string pattern with another everywhere in the string.
e.g.
'ab cd ab ab'.replace('ab', 'xy')
# produces output 'xy cd xy xy'
similarly,
mystr = 'ab cd ab ab'
mystr.replace(mystr[0:2], 'xy')
# also produces output 'xy cd xy xy'
what you could do instead, to replace just the characters in position 22-26
line = line[0:22] + new_value + line[26:]
Also, looking at your data, it seems to me to be a fixed-width text file. While my suggestion will work, a more robust way to process this data would be to read it & separate the different fields in the record first, before processing the data.
If you have access to the pandas library, it provides a useful function just for reading fixed-width files

need guidance with basic function creation in MATLAB

I have to write a MATLAB function with the following description:
function counts = letterStatistics(filename, allowedChar, N)
This function is supposed to open a text file specified by filename and read its entire contents. The contents will be parsed such that any character that isn’t in allowedChar is removed. Finally it will return a count of all N-symbol combinations in the parsed text. This function should be stored in a file name “letterStatistics.m” and I made a list of some commands and things of how the function should be organized according to my professors' lecture notes:
Begin the function by setting the default value of N to 1 in case:
a. The user specifies a 0 or negative value of N.
b. The user doesn’t pass the argument N into the function, i.e., counts = letterStatistics(filename, allowedChar)
Using the fopen function, open the file filename for reading in text mode.
Using the function fscanf, read in all the contents of the opened file into a string variable.
I know there exists a MATLAB function to turn all letters in a string to lower case. Since my analysis will disregard case, I have to use this function on the string of text.
Parse this string variable as follows (use logical indexing or regular expressions – do not use for loops):
a. We want to remove all newline characters without this occurring:
e.g.
In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
In my younger and more vulnerableyears my father gave me some advicethat I’ve been turning over in my mindever since.
Replace all newline characters (special character \n) with a single space: ' '.
b. We will treat hyphenated words as two separate words, hence do the same for hyphens '-'.
c. Remove any character that is not in allowedChar. Hint: use regexprep with an empty string '' as an argument for replace.
d. Any sequence of two or more blank spaces should be replaced by a single blank space.
Use the provided permsRep function, to create a matrix of all possible N-symbol combinations of the symbols in allowedChar.
Using the strfind function, count all the N-symbol combinations in the parsed text into an array counts. Do not loop through each character in your parsed text as you would in a C program.
Close the opened file using fclose.
HERE IS MY QUESTION: so as you can see i have made this list of what the function is, what it should do, and using which commands (fclose etc.). the trouble is that I'm aware that closing the file involves use of 'fclose' but other than that I'm not sure how to execute #8. Same goes for the whole function creation. I have a vague idea of how to create a function using what commands but I'm unable to produce the actual code.. how should I begin? Any guidance/hints would seriously be appreciated because I'm having programmers' block and am unable to start!
I think that you are new to matlab, so the documentation may be complicated. The root of the problem is the basic understanding of file I/O (input/output) I guess. So the thing is that when you open the file using fopen, matlab returns a pointer to that file, which is generally called a file ID. When you call fclose you want matlab to understand that you want to close that file. So what you have to do is to use fclose with the correct file ID.
fid = open('test.txt');
fprintf(fid,'This is a test.\n');
fclose(fid);
fid = 0; % Optional, this will make it clear that the file is not open,
% but it is not necessary since matlab will send a not open message anyway
Regarding the function creation the syntax is something like this:
function out = myFcn(x,y)
z = x*y;
fprintf('z=%.0f\n',z); % Print value of z in the command window
out = z>0;
This is a function that checks if two numbers are positive and returns true they are. If not it returns false. This may not be the best way to do this test, but it works as example I guess.
Please comment if this is not what you want to know.

Renaming a structure in MATLAB using an existing string

I have a structure in MATLAB called dat. I want to rename dat as an existing string.
Existing_str='NewName'
$(Existing_str)=dat
This fails as I don't think MATLAB lets me use the dollar sign in this way. The code below creates a copy of dat literally called Existing_str and destroys the Existing_str in the process.
Existing_str=dat
While the code below generates a collosal empty structure which clearly is not a copy!
eval(Existing_str)=dat
In the task I am actually trying to perform I don't know the name of the existing_str in advance so that is not a solution.
You were almost there with your `eval'. What you want is:
eval([Existing_str '=dat;']);
This works because you're composing a string inside your square brackets. If you just looked at the resulting string, it would look like NewName=dat; The eval command simply tells Matlab to evaluate the string as if you typed it into the command line.
You can use dynamic field naming (Bas's suggestion), and avoid eval:
For example, if you have just loaded a structure dat from a file 'somefile.ext' with some custom parsing function:
filename = 'somefile.ext'; % presume you actually have a list of files from dir or ls
dat = yourfunction(filename);
[~, name, ~] = fileparts(filename);
alldat.(name)=dat;
This is equivalent to:
alldat.somefile = dat;
Except that we've just automatically taken the name from the filename (in this case just by stripping off the path/extension, but you could do other things depending on the pattern of the filename).
The bonus of this is that you can then, say, with a structure that has fields alldat.file1, alldata.file2, alldat.file3, all of which have a subfield, say, size do things like this:
names = fieldnames(alldat)
for n = 1:length(names)
alldat.(names{n}).mean = mean(alldata.(names{n}).size);
end
Every sub-structure now has a field, mean, which contains the mean of the data. If you had a bunch of different named structures you would need to eval everything you wanted to do to them collectively, and the code becomes difficult to read and maintain.
The other option is a cell array. Here's an easy trick:
dat = % whatever you do to make this structure
alldat{end+1} = dat;
This just appends the new dat onto the end of an existing cell array. {end+1} ensures it doesn't overwrite existing data.

Resources