Matlab error when playing a sound - matlab

I'm using matlab gui, I'm recording a sound then save it in a folder in c, then display the recorded sound that are in the folder in a listbox when I press play on the sound wav. Matlab gives an error the error is :
************Error using audioread (line 74)**
***The filename specified was not found in the MATLAB path.
Error in Monitoring_System>play_Callback (line 178)
[q, Fs] = audioread(thisstring);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Monitoring_System (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in #(hObject,eventdata)Monitoring_System('play_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback*************
-The record code:
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(33),a(34));
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);
fullpath=fullfile('c:\monitoringsystem',name);
wavwrite(y,44100,fullpath);
y=[];
The code to diplay them in the listbox:
d = dir('C:\monitoringsystem\*.wav'); %get files
set(handles.listbox1,'String',{d.name})
The code to play the sound that is chosen from the listbox:
allstrings = cellstr( get(handles.listbox1, 'String') );
curvalue = get(handles.listbox1, 'Value');
thisstring = allstrings{curvalue};
[q, Fs] = audioread(thisstring);
soundsc(q,44100);
Any help how to solve this problem, with keeping to save in a specific folder. I copied the recorded sound to the matlab folder and then pressed play in the gui for that wav sound it didin't give any error.

Did you try debugging this and seeing what d contains after selecting a file?
As per the documentation, d = dir('C:\monitoringsystem\*.wav'); returns a struct with the following fields: name, date,bytes, isdir, datenum (at least on MATLAB 2015a). Although {d.name} gives you the filename correctly, you should note that this is a relative path only, so MATLAB doesn't where to look for the file unless it's in the active directory.
I'm not entirely sure why you went through all the trouble with allstrings, curvalue, and thisstring, but if I understand correctly what you're trying to do, I would suggest one of two approaches:
Define the default path (i.e. C:\monitoringsystem) in a constant, and then use it when saving\loading:
DEFAULT_PATH = `C:\monitoringsystem`; %// Definition
...
fullpath = fullfile(DEFAULT_PATH,name); %// When saving
...
d = dir(fullfile(DEFAULT_PATH,'*.wav')); %// When listing files
...
[q, Fs] = audioread(fullfile(DEFAULT_PATH,{d.name})); %// When reading a file
Using uigetfile:
[FileName,PathName] = uigetfile('*.wav','Select the WAV file');
FullPath = fullfile(PathName,FileName);
(and then the rest is very similar to the 1st case)

Related

How to get the audioread.m file again in the matlab?

I have edited the audioread.m file
I have used
which('audioread', '-all')
rmpath(fileparts(which('audioread')))
savepath
Now I have got an error
Undefined function or variable 'audioread'.
I have checked with
[d,sr] = audioread('sadness22.wav');
% Plot the spectrogram
subplot(311)
specgram(d(:,1),1024,sr);
% Read in a different format
[d2,sr] = audioread('piano.mp3');
subplot(312)
specgram(d2(:,1),1024,sr);
Can you please give any suggestions regarding this issue?

MATLAB code for combining .fig files from a folder to generate a video/ animation?

I am trying to make an animation from a sequence of figures (.fig) files of MATLAB from a folder in my system. I referred to the questions here and here.
Finally, I have the code mkvideo.m :
% Creating Video Writer Object
writerObj = VideoWriter('peqr.avi');
% Using the 'Open` method to open the file
open(writerObj);
% Creating a figure.
% Each frame will be a figure data
Z = peqr;
surf(Z);
axis tight
set(gca,'nextplot','replacechildren');
set(gcf,'Renderer','zbuffer');
[figures,var] = uigetfile('*.fig','Multiselect','on');
for k = 1:length(figures)
Multi_Figs = [var,filesep,figures{k}];
Op = openfig(Multi_Figs);
% Frame includes image data
frame = getframe;
% Adding the frame to the video object using the 'writeVideo' method
writeVideo(writerObj,frame);
close(Op);
end
% Closing the file and the object using the 'Close' method
close(writerObj);
I am getting the following error
Warning: No video frames were written to this file. The file may be invalid.
> In VideoWriter/close (line 278)
In VideoWriter/delete (line 213)
In mkvideo (line 2)
Undefined function or variable 'peqr'.
Error in mkvideo (line 8)
Z = peqr;
I think this code should be able to produce a video once in user interface, I select the files?
It would be helpful if I could get some help to rectify the error in this code so that the animation or video making is possible.

How to save multiple file (.mat) into .wav using MATLAB

I have a .mat file containing 100files. How to convert the 100 files one by one to .wav.
Every file contain vectors.I tried using this code but I got errors.
x=load('data_cropped.mat');
input_list = x;
for i = 1:length(input_list)
fid = fopen(input_list(i).name);
data = ' ';
fopen(fid);
wavwrite(data,16000,[input_list(i).name(1:length(input_list(i).name)-3),'wav']);
clear data
end
The error is:
>> convert_to_wav
Reference to non-existent field 'name'.
Error in convert_to_wav (line 7)
fid = fopen(input_list(i).name);
Please help me,
Thanks a lot
Assuming you know the sample rate of your audio then the snippet below should do what you want and give you a series of numbered wav files.
clear
load('data_cropped.mat');
data = whos;
fs = 44100 %change to your sample rate
for i = 1:length(data)
wavwrite(data(i).name,fs,num2str(i));
end

save figure on multiple folders on matlab

I am new on matlab and I don't know many things at the moment.
I have a script which create more than 100 figures. I want to save those figures in 3 different folders. Here is my code until now:
pridir='C:\Users\tasos\Desktop\folder';
figtxt1='folder1';
figtxt2='folder2';
figtxt3='folder3';
yM = load('pathtomydata');
[n,m]=size(yM);
maxtau2 = 10;
alpha = 0.05;
zalpha = norminv(1-alpha/2);
p=6;
for i=1:m-1
for j=i+1:m
figure()
y1V=yM(:,i);
y2V=yM(:,j);
plot(y1V,'b')
hold on
plot(y2V,'r')
legend(sprintf('text= % d',i),sprintf('text= % d',j))
title('My Title')
printto = sprintf('%s%d\\text%d and %d.jpg',pridir,i,i,j);
print('-djpeg90',printto)
close(gcf)
end
end
If I left my code like this, all the figures created but they didn't save on folders. If I remove the "%" from the last two lines, I have the following error
Error using name (line 103)
Cannot create output file 'C:\Users\tasos\Desktop\folder1\text1 and 2.jpg'
Error in print (line 206)
pj = name( pj );
Error in test (line 25)
print('-djpeg90',printto)
P.S. I am using the R2012b version
Avoid eval()!
Define one generic print directory:
pridir = 'C:\Users\***\Desktop\fold';
Then inside the inner loop:
printto = sprintf('%s%d\\figuretext %d and %d.jpg',pridir,i,i,j);
print('-djpeg90',printto)
where printto will be something like:
C:\Users\***\Desktop\fold1\figuretext 1 and 3.jpg
Also, you might want to close the figure after the print: close(gcf).
NOTE: the directories where you're gonna save the files should already exist, otherwise create them with mkdir() before saving any pictures.
I think matlab changed the code for writing a folder with the 2013 version.
I changed 'XX\YY\ZZ.pjg'
To:
'XX/YY/ZZ.pjg'
And it worked for me, strangely.

Converting tiff images to avi in matlab

I'm trying to use VideoWriter to convert large amounts of tiff images into an avi video. I'm having trouble with the for loop to convert the images into a video. Here is the code I have so far:
function tif2avi
clc; close all;
[imagelist,p]=uigetfile('*.tif','MultiSelect','on',...
'Select LIST to plot'); pause(0.5); cd(p);
if ~iscell(imagelist); disp('imagelist not cell'); return; end;
outputVideo = VideoWriter('0424_rat01.avi');
outputVideo.FrameRate = 16;
outputVideo.Quality = 100;
open(outputVideo);
for i=1:numel(imagelist)
img=imread(imagelist(i));
writeVideo(outputVideo,img);
end
Can anyone help me out? I think the problem is in my for loop.
This is the error I get when I try to run the code:
Warning: No video frames were written to this file. The file may be invalid.
> In VideoWriter.VideoWriter>VideoWriter.close at 289
In VideoWriter.VideoWriter>VideoWriter.delete at 238
Error using imread>parse_inputs (line 476)
The filename or url argument must be a string.
Error in imread (line 335)
[filename, fmt_s, extraArgs] = parse_inputs(varargin{:});
Error in tif2avi (line 14)
img=imread(imagelist(i));
Your imagelist is a cell array and therefore should be accessed using curly braces (instead of regular parentheses).
Replace
img = imread( imagelist(i) );
with
img = imread( imagelist{i} );
and see what happens.
PS,
It is best not to use i as a variable name in Matlab.