Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
enter image description hereThe wave file are like:The first digit- speaker id, second digit-utterence id
00
01
02
03
04
.
.
.
59
I tried using
for m=0:5
for y=0:9
file=sprintf('%s%d%d','E:\0 (1)\0\',m,y);
[s,fs]=wavread(file);
end
end
But this gives error. Alternatively I tried
mypath = 'E:\\0 (1)\\0\\';
filename = sprintf([mypath '%s%d.wav'],m,y);
[s,fs] = audioread(filename);
But it gave error as :
Function is not defined for sparse inputs.
Please help me.
It seems that your problem comes from string concatenation. But since neither your file format nor your path format are clear, this is just a sample code that performs something close to what you are attempting to do:
for m = 0:5
m_curr = num2str(m);
for y = 0:9
file = ['E:\' m_curr num2str(y) '.wav'];
[s,fs] = wavread(file);
end
end
Set a debug breakpoint on the wavread line and check if the format matches your needs. If not, change it accordingly.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a folder 'a' with about 200 files with names xx_out_02.csv and I want to rename them to xx_out.csv. May be using Matlab or running some script. I tried it in cmd but I have to run the command for each and every file.
Can someone help me here?
Best Regards
Dilip
You can use the movefilefunction from matlab.
Here is an example:
clc
addpath('yourdir')
csvf = dir('yourdir/*.csv');
numberOfcsv = numel(csvf);
for ii = 1:numberOfcsv
file = csvf(ii).name;
movefile(sprintf('yourdir/%s', file), sprintf('yourdir/x%03d_out.csv', ii), 'f');
end
Your question is unclear. I'm assuming
You want to strip off substrings of the form _ followed by one or more digits right before .csv.
The resulting target names are all different. For example, you have files such as xx_out_02.csv and yy_out_01.csv, but not xx_out_02.csv and xx_out_01.csv.
Operating system? I'm considering Windows. For other systems you can change the system line below with the appropriate system comand. Or better use movefile as in SamuelNLP's answer.
Code:
files = dir('*.csv');
names = {files.name};
for n = 1:numel(names)
name = names{n};
name_new = regexprep(name, '_\d+(?=\.csv$)', '');
system(['ren ' name ' ' name_new]); %// MS-DOS command to rename file
end
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have an array in the third element dataArr[2], I know it contains a 10 digit phone. I need to only read or print the first 6 digits. For instance if my phone is 8329001111, I need to print out the 832900. I tried to see if I can use substr but I keep reading or printing the full list. Do I need to dereference..
Try this :
$dataArr[2] =~ s/\s//g; # ensure there's no spaces
print substr($dataArr[2], 0, 6);
# ^ ^ ^
# variable | |
# offset start|
# |
# substring length
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to find a way to extract text in a specific and efficient way
as in this example:
'Hello Mr. Jack Andrew , your number is 894Gfsf , and your Bank ID # 734234"
I want a way to get the Name, the Number and the Bank ID Number.
I want to write software that deals with different text files and get those required values. I may not know the exact order but it must be a template like a bank statement or something.
Thanks!
It's a bit hard to understand what exactly is the problem.. If all you need to do is to split strings, here's a possible way to do it:
str = 'Hello Mr. Jack Andrew , your number is 894Gfsf , and your Bank ID # 734234';
tokenized = strsplit(str,' ');
Name = strjoin([tokenized(3:4)],' ');
Number = tokenized{9};
Account = tokenized{end};
Alternatively, for splitting you could use regexp(...,'split') or regexp(...,'tokens');
I think you want regular expressions for this. Here's an example:
str = 'Hello Mr. Jack Andrew , your number is 894Gfsf , and your Bank ID # 734234';
matches=regexp(str, 'your number is (\w+).*Bank ID # (\d+)', 'tokens');
matches{1}
ans =
'894Gfsf' '734234'
My suggestion would be to make a whole array of strings with sample patterns that you want to match, then build a set of regular expressions that collectively match all of your samples. Try each regexp in sequence until you find one that matches.
To do this, you will need to learn about regular expressions.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to Matlab and the answer may be very simple. I have script that runs a function to return an answer matrix, ans. How can I get Matlab to return a matrix named J instead?
Here is how I call the function: (myfunction(a,b));
I get the following error if i try to call 'myfunction = J'.
Error using myfunction (line 10)
Not enough input arguments.
Error in myfunction (line 25)
J = myfunction
If remove the line myfunction = J. I no longer get an error in line 10 any more.
Thanks
The problem was I was trying to name the output in the function where as I should have defined this in my script.
SO instead of calling the function as:
(myfunction(a,b));
Instead it should be: J=(myfunction(a,b));
It depends on how you call your script.
If you do:
> myScript;
in the command window, the result will be stored in the variable ans.
If you do:
> J = myScript;
The result will be stored in J.
Whatever function you have it should have at least one output in your case (because you say that you expect something). So for instance if your function returns one variable write it as
[T]=myfunction(a,b);
so in this case T is the name of your output instead of "ans". You need to write your function in a separate .m file and save it under the same name as is the name of function, so in this case you need to save it as myfunction.m file. It has to be in the same folder as your main code is.
See the link below
http://www.mathworks.com/help/matlab/ref/function.html
This question already has answers here:
How can I load 100 files with similar names and/or string in just one step in MATLAB?
(3 answers)
Closed 9 years ago.
As Shai and CTZStef suggested, when I have to open multiple files with similar names in MATLAB, I have to do
for k=1:size(fls)
fileName = strcat('int_',num2str(k),'.ASC');
A(k) = importdata(fileName, ' ', 9);
x(k) = A(k).data(:,1);
y(k) = A(k).data(:,2);
end
or also
fls = dir('int_00_*.ASC');
for fi=1:numel(fls)
A(fi) = importdata(fls(fi).name, ' ',9);
end
Well, the problem is that none of them works.
What should I do? Is it a problem with my MATLAB version?
You need to follow the answers you got more closely:
The strcat solution CANNOT handle the zero padding of the file names. You'll ahve to manually rename all files from 'int_001.ASC' to 'int_1.ASC'.
UPDATE (due to #DedekMraz's comment): you'll need to modify the strcat input string to strcat('int_', num2str( k, '%03d' ), '.ASC');
You can use a strategy similar to this one. See the update to the answer you already got.
The input you gave your dir function is wrong it has to be dir('int_*.ASC') and not dir('int_00_*.ASC').