Error using wavread Invalid Wave File. Reason: Cannot open file - matlab

i got error Error using wavread (line 67)
Invalid Wave File. Reason: Cannot open file.
Error in energy_per_frame (line 2) [inpu fs] = wavread(file);
this is the source code for function that call energy_per_frame.
[inpu fs] = wavread(file);
clc;clear;close all;
file = input('Enter name of wav file ', 's');
counter=0;
for test_number = 0:4
for test_number2 = 0:20
counter=counter+1;
reference = dtw_avg(test_number2);
test = energy_per_frame(file);
distance(counter) = dtw(test,reference);
end
This is source code for function energy_per_frame
[inpu fs] = wavread(file);
fn = fs/2;
bins = 512; %useable bins
frame_length = bins;
window = hamming(frame_length);
overlap = .5;
number_of_frames = (length(inpu)/(frame_length)/overlap) - 1;
What is wrong here?

Well, your function definition should look something like this
function [ output_args ] = energy_per_frame( input_args )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
end
It looks like your missing the function keyword.
However, if the first block is just supposed to be a script then you're making the call to wavread before you actually have the user input for the filename. Also, there is really no need to call clear etc. like that in a function.

Related

Dimensions are not Ok in matlab

When I take image from img from commands =
imread('C:\Users\Administrator\Desktop\Detect\F04U2MPIXQG15WQS.LARGE.jpg');
figure,imshow(img)
It works fine but when I use
[filename, pathname] = ...
uigetfile({'*.jpg';'*.jpeg';'*.png';'*.*'},'Select Image File');
img=strcat(pathname,filename);
error shown is Error in skinDetect2Func (line 8)
yuv(:,:,y) = (img(:,:,r)+2.*img(:,:,g)+img(:,:,b))/4; Error in
bwFingers1 (line 39)
out=skinDetect2Func(img);
Anyone can help me out I am just stuck in here. :(
Here is the complete code:
function out=skinDetect2func(img)
imshow(img);
sz=size(img); r=1;g=2;b=3;y=1;u=2;v=3;
yuv=img;
region=yuv;
yuv(:,:,y) = (img(:,:,r)+2.*img(:,:,g)+img(:,:,b))/4;
yuv(:,:,u) = img(:,:,r)-img(:,:,g);
yuv(:,:,v)=img(:,:,b)-img(:,:,g);
region = (yuv(:,:,u)>20 & yuv(:,:,v)<74) .* 255;
toc;
out=region;
%filtering
out=im2bw(out); out=bwareaopen(out,100);
out=imdilate(out,strel('diamond',4));
%retain largest only
res=out;
cc=bwconncomp(res);
arr=(cellfun('length',cc.PixelIdxList));
newLabel=res;
if ~isempty(round(arr))
msz=0;
for i=1:length(arr)
if msz<arr(i:i)
msz=arr(i:i);
index=i;
end
end
labels=labelmatrix(cc);
newLabel=(labels==index);
out=newLabel;
end
out=imfill(out,'holes');
end***
ehm,
This is a string:
img=strcat(pathname,filename); % STRing conCATenation
You still need to imread...
The correct code would be
imgpath=strcat(pathname,filename);
img=imread(imgpath);

How to print the current filename in a plot in matlab?

I have one .m file per plot and want to see in my draft printouts, which file was used to create it.
This should be done with a function which can be placed in my .m file and
commented out, for the final version.
% addWatermarkFilename() %
So far I found mfilename(), but it could not get the name of the calling function. I am looking also for a good way to put the text in the picture without changing the size.
Solution:
I combined the suggestions by Luis Mendo and NKN to:
function [ output_args ] = watermarkfilename( )
% WATERMARKFILENAME prints the filename of the calling script in the
% current plot
s = dbstack;
fnames = s(2).name;
TH = text(0,0,fnames,'Interpreter','none');
TH.Color = [0.7 0.7 0.7];
TH.FontSize = 14;
TH.Rotation = 45;
uistack(TH,'bottom');
end
I suggest instead of commenting out a function from the code, use a proper flag. For instance the code can be something like this:
clc,clear,close all
addWaterMark = true; % if true you will get the filename in the title
fname = mfilename; % get file name mfilename('fullpath') for the full-path
t=-2*pi:0.1:2*pi;
y = sin(t);
plot(t,y); grid on;
xlabel('t');ylabel('y');
if addWaterMark
title(['filename: ' fname '.m']);
else
title('plot no.1');
end
A little bit playing with the text function, you can make a proper watermark. something like this:
clc,clear,close all
addWaterMark = true;
fname = mfilename;
fnames = [fname '.m'];
t=-2*pi:0.1:2*pi;
y = sin(t);
plot(t,y); grid on;
xlabel('t');ylabel('y');
if addWaterMark
title(['filename: ' fnames]);
t = text(-3,-0.4,fnames);
t.Color = [0.7 0.7 0.7];
t.FontSize = 40;
t.Rotation = 45;
else
title('plot no.1');
end
Note: Obviously the code between the if and else can be stored as a function that receives the string (char) from the fnames variable and a handle for the figure.
If you want to get the name of the function or script that called the current function or script, use dbstack as follows:
s = dbstack; % get struct with function call stack information
callerName = s(2).name; % get name of second function in the stack
The first entry in s refers to the current function; the second refers to the one that called it.

What could be wrong with the file path in the following code?

I understand that this could be due to a wrong file path but I have tried all the ways to correct it and still ending up with the same error. The code is given below.
clc
clear all
fid=fopen('ECresult.txt','w');
for p=1:2;
% textFilename{p} = fullfile('/ccc/Desktop/Lalitha-18/EO',['EO' num2str(p)'.txt']);
textFilename{p} = fullfile('C:/Users/Biswajit/Desktop/Biswajit_nimmy',['LC1EC1' num2str(p) '.txt']);
id{p} = fopen(textFilename{p},'rt');
textdata{p} = textscan(id{p},'%s%s');
fclose(id{p});
p
X=load(['EC' num2str(p) '.txt']);
N=length(X)
m=2;
counter1=0; counter2=0;
k=0.2;
r = k*std(X);
i=1;j=1;
while i<= N-m
Y=[X(i:i+m-1)];
while j<=N-m
Z=[X(j:j+m-1)];
d1=abs(Y-Z);
if d1<=r
counter1=counter1 + 1;
end
j=j+1;
end
IC(i) = counter1/(N-m);
counter1=0;j=1;
i=i+1;
end
i=1;
m=m+1;
while i <= N-m
Y = [X(i:i+m-1)];
while j<= (N-m)
Z = [X(j:j+m-1)];
d2 = abs(Y-Z);
if d2<=r
counter2=counter2 +1;
end
j=j+1;
end
ID(i)=counter2/(N-m);
counter2=0; j=1;
i = i+1;
end
Entropy=0;
i=1;
while i<=N-m
Ratio=IC(i)/ID(i);
Entropy=Entropy +log (Ratio);
i = i+1;
end
Approxntropy(p) = Entropy/(N-m);
fid=fopen('EO.txt','w');
fprintf(fid, '%5.6f\r\n', Approxntropy);
end
The exact error message is as below
"
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in Apensdploop (line 13)
textdata{p} = textscan(id{p},'%s%s');
"
Please advise.
Your error message means that fopen was unable to open the file. In this case, it returns -1. Since you didn't catch the error, you passed -1 to textscan which then complains. Therefore, before passing id{p} to textscan, you should check if it is -1, which means an error.
What you are passing to fopen is
fullfile('C:/Users/Biswajit/Desktop/Biswajit_nimmy',['LC1EC1' num2str(p) '.txt']);
which for p = 1 becomes
'C:/Users/Biswajit/Desktop/Biswajit_nimmyLC1EC11.txt'
Looks like a slash is missing to me. Try
fullfile('C:/Users/Biswajit/Desktop/Biswajit_nimmy/',['LC1EC1' num2str(p) '.txt']);
instead?
Otherwise, set a breakpoint at the fopen like and look at the content of textFilename{p}. Then you will see if it contains the correct path and file name or not.

MatLab - Error using run => Input argument is undefined

As a MatLab newbie, I have problems running the following script:
function [ newarray ] = reshapeIm( array, period )
%reshapeIm(array, period) Summary of this function goes here
% Detailed explanation goes here
le = length(array);
fra = le/period;
array = [array, zeros(1, ceil(fra)*period-le)];
newarray = reshape(array', period, []);
end
load('1200rpm_shak3.mat');
cRounds = 54;
mylength = 100;
thetas = (1:cRounds*mylength).*2*pi/mylength;
thetas0 = (1:mylength).*2*pi/mylength;
figure;
plot(z1(1:300), '.-');
plot(z2(1:300), '.-');
z1s = z1;
z2s = [z2(mylength/4+1:end) z2(1:mylength/4)];
z3s = [z3(mylength/2+1:end) z3(1:mylength/2)];
z4s = [z4(mylength*3/4+1:end) z4(1:mylength*3/4)];
dr = 1/4.*(z1s+z2s+z3s+z4s); % gemittelt
drs = reshapeIm(dr, mylength);
drs_std = std(drs, 1, 2);
drs_meanstd = mean(drs_std);
figure;
polar(thetas, 250000+200*dr);
figure;
polar(thetas0', 250000+200*mean(drs,2));
The command window says:
??? Error using ==> run
Input argument 'array' is undefined.
I suppose, that's because the script was written for a newer MatLab, but I use 5.3.
Functions need to be in their own files. You cannot define functions within a script file. So, to get your code to work at all, you need to move the function that is at the top of your file (which I copied below) into its own file. Name the file "reshapeIM.m" and save it in the same directory where you are doing your work.
function [ newarray ] = reshapeIm( array, period )
%reshapeIm(array, period) Summary of this function goes here
% Detailed explanation goes here
le = length(array);
fra = le/period;
array = [array, zeros(1, ceil(fra)*period-le)];
newarray = reshape(array', period, []);
end
Then, after removing the function from your script, save your script and run it again. This should solve the problem with regards to the function. You may have other errors, but this should take care of the error that you reported.

Undefined function 'relabel' for input arguments of type 'int32'. Error in import_experiment_label (line 22) runs = relabel(run);

I got an error when I run a matlab code online. The error is:
Warning: Name is nonexistent or not a directory: ......\toolbox_misc
Undefined function 'relabel' for input arguments of type 'int32'.
Error in import_experiment_label (line 22)
runs = relabel(run);
I tried to download matlab toolbox misc online, but still cannot fix
the problem. Anyone can help me? Thank you so much!
The following is the original code:
% Load the text experiment-label file to cell
% Kinalizer: /share/Bot/Research/mvpa_data/Haxby_6_subjects
% mac: /Users/kittipat/Downloads/Research/Haxby_7_subjects
% subjID = 2;
inDir = ['/Users/kittipat/Downloads/Research/Haxby_7_subjects/subj',num2str(subjID),'/'];
inFile = 'labels.txt';
outDir = ['/Users/kittipat/Downloads/Research/Haxby_7_subjects/subj',num2str(subjID),'/matlab_format'];
fileID = fopen(fullfile(inDir,inFile));
% !!!!! Must remove "labels chunks" at the top of the txt file first
myCell = textscan(fileID, '%s %d');
fclose(fileID);
category_name = myCell{1};
run = myCell{2};
% Make sure the run numbers are well-ordered from 1 to R
addpath('../../../toolbox_misc/');
runs = relabel(run);
num_run = length(unique(runs));
num_time_stamp = length(runs);
% Make associate labels (needs input from user)
category_name_list = {'rest','face','house','cat','bottle','scissors','shoe','chair','scrambledpix'};
assoc_label_list = [0,1,2,3,4,5,6,7,8];
num_category = length(assoc_label_list); % including 'rest'
assoc_label = zeros(num_time_stamp,1);
regs = zeros(num_category,num_time_stamp);
for i = 1:num_time_stamp
assoc_label(i) = assoc_label_list(strcmp(category_name{i},category_name_list));
regs(assoc_label(i)+1,i) = 1; % 'rest' is column 1
end
regs_with_rest = regs;
regs = regs(2:end,:); % exclude "rest" in the 1-st column
num_category = num_category - 1; % exclude the "rest"
save(fullfile(outDir,'experiment_design'),...
'category_name',...% the category name for each time stamp
'assoc_label',...% the number label for each time stamp
'assoc_label_list',...% the mapping between category_name and assoc_label
'category_name_list',...% list of the category name
'num_category',...% number of categories excluding "rest"
'regs',...% the category matrix excluding "rest"
'num_run',...% number of runs in well-ordered integer
'runs'... % the run# for each time stamp
);
%% plot the figure
h1 = figure;
subplot(4,1,2); plot(assoc_label,'b.-');
xlim([1, num_time_stamp]);
set(gca,'YTick',0:max(assoc_label(:))); set(gca,'YTickLabel',category_name_list);
subplot(4,1,1); plot(runs,'r.-');
title('run number after relabeling --> runs'); xlim([1, num_time_stamp]);
subplot(4,1,3); imagesc(regs_with_rest);
title('original design matrix --> regs\_with\_rest');
set(gca,'YTick',1:(num_category+1)); set(gca,'YTickLabel',category_name_list);
subplot(4,1,4); imagesc(regs);
title('after "rest" is removed --> regs');
xlabel('time stamps');
set(gca,'YTick',1:num_category); set(gca,'YTickLabel',category_name_list(2:end));
print(h1,'-djpeg',fullfile(outDir,'experiment_design.jpg'));
I fix the relabel problem. Then I change part of the code as follows.
inDir = ['/D disk/MATLAB/R2014a/subjX/beta_extraction_for_Haxby_matlab_toolbox_v1_8/subj',num2str(subjID),'/'];
inFile = 'labels.txt';
outDir = ['/D disk/MATLAB/R2014a/subjX/beta_extraction_for_Haxby_matlab_toolbox_v1_8/subj',num2str(subjID),'/matlab_format'];
Another error comes:
import_experiment_label
Undefined function or variable 'subjID'.
Error in import_experiment_label (line 7)
inDir = ['/D disk/MATLAB/R2014a/subjX/beta_extraction_for_Haxby_matlab_toolbox_v1_8/subj',num2str(subjID),'/'];
How to fix this problem? I do not know what is the wrong here. Thank you guys!