I have writtring a script that convert a set of BMPs to avi. up until recently it worked fine. now I get this wierd error "Failed to write stream data". I get it after converting 5 libraries of bmps to avi. It runs over librarirs of BMPs and convert each library to avi. each time it stacks in the 6th movie.. there are no corrupts files in the 6th library. any idea why?
this is the code:
%this works
clc
%path='C:/Documents and Settings/Ariel/Desktop/exp_brk_scrm/2.1/group1/exp_up/exp_up/4python/stims';
%FullPath=strcat(path,'/mov1.avi');
path4avi='G:/experiments/cfs3/building/Copy of StimBMP/avi/'; %dont forget the in the end of the path
pathOfFrames='G:/experiments/cfs3/building/Copy of StimBMP/stims/'; %here too
NumberOfFiles=70; %to be generated
NumberOfFrames=8; %in each avi file
for i=1:1:(NumberOfFiles)
FileName=strcat(path4avi,'Stim',int2str(i),'.avi') %the generated files
aviobj = avifile(FileName,'compression','None'); %due to changes in the new Media Players
aviobj.fps=10;%10 frames in Sec
for j=1:1:(NumberOfFrames)
Frame=strcat(pathOfFrames,'stim',int2str(i),'/stim',int2str(j),'.BMP') % the BMP's (not a good name for thedirectory)
%[Fa,map]=imread(Frame);
%imshow(Fa,map); %
[Fa,map]=imread(Frame);
imshow(Fa,map);
% imshow(Fa);
F=getframe();
aviobj=addframe(aviobj,F)
end
aviobj=close(aviobj);
end
Hi I know this might seem a bit over simplified but I had the same issue.
My code worked fine and just one day stopped exactly as you have described. I found that it was just the destination I was writing my files to simply did not have enough memory for the video files. Deleted some junk I didn't need and it worked instantly. Matlab just doesn't realise the issue is with storage space so in my case it said there was an issue with its own 'movie2avi' function
Since I'm not sure what the source of your problem is, I'm just providing a simple working example of how to create an AVI movie. Demo images from the Image Processing Toolbox are used:
figure('Color','white')
aviObj = avifile('out.avi', 'fps',5); %# create AVI object
for i=1:10
I = imread( sprintf('AT3_1m4_%02d.tif',i) ); %# read image frame
imshow(I, 'Border','tight'), colormap gray %# show image
aviObj = addframe(aviObj, getframe(gcf)); %# grab frame and add to AVI
end
close(gcf)
aviObj = close(aviObj); %# close and write movie
winopen('out.avi') %# play movie in Windows
Does the order of the libraries matter? In other words, if you run the 6th first and the 1st last, will it crash on the first or on the last?
If it crashes on the first, then you library #6 has a problem
If it crashes on the last, you may be filling up memory somehwere. Use clear classes before running your script, which should eliminate whatever Matlab is filling up in the memory. Alternatively, if the leak or fragmentation is really bad, you could try and restart Matlab after three libraries.
Related
I'm a novice MATLAB user, so apologies if the question is very basic. I need a .wav sound file to be played in a single, specific channel -- let's say the left channel. As it is, my code reads in the sound file, and I add in a column of zeros to nullify the channel I don't want, like so:
currentDir = pwd;
soundFile = [currentDir '\sound1.wav']; % load the file
[y, Fs] = audioread(soundFile); % read the file in
soundData(:,1) = y(:,1); % keeps sound for the left channel
soundData(:,2) = 0; % nullifies the right channel
sound = audioplayer(soundData,Fs);
play(sound);
As it stands, the code currently produces a sound that is full volume in the left speaker, and half volume (but still very much audible) in the right speaker. I have tried this with at least 20 .wav files, with the same result.
In case it's relevant, this happens even when I write in code that explicitly matches the length of the sound variable in 0s, like so:
[y, Fs] = audioread(soundFile);
silentChannel = zeros(size(y));
soundData(:,1) = y(:,1); % keeps sound for the left channel
soundData(:,2) = silentChannel(:,2); % nullifies the right channel
Does anybody know what I'm doing wrong, or have any thoughts?
Your code is definitely correct and it should only play audio in the left channel. I suspect that the problem is caused by a sound-card/driver issues. Allow me suggest the following troubleshooting steps:
Save your output as a wav file using audiowrite('output.wav', soundData, Fs). Play this using a different audio player, such as Audacity. If you still hear output in both channels, it must be a sound-card/driver issue.
Assuming that you are using a Windows PC (going by the syntax in your file path), make sure all sound enhancements are disabled. How to do this depends on the PC. If there is a third-party app controlling the playback settings, you'd have to use that. Otherwise find the settings shown in the picture below in the Control Panel.
In MatLab the expected method for playing sound is the method sound(data,Fs)
To control the channel the sound emits on, you'll want to know how sound() reads data.
data is a matrix with the columns representing channels, and with the rows holding the samples of the waveform for a given sampling fequency Fs
here is a simple implementation.
function treismanwolfe()
close all
clear all
clc
Fs = 40000;
tau = 2*pi();
t = 0:tau/(Fs-1):tau/2;
left = sin(t).*(sin(t*200)+sin(t*1600));
left= left/max(abs(left));
left = left'; %turn column vector into row
right = sin(t).*(sin(t*800)+sin(t*400));
right= right/max(abs(right));
right = right'; %turn column vector into row
data = [left,right*0]; %multiply either by 0 to nullify
sound(data,Fs); %so you can hear it.
end
I hope this works for you. Enjoy!
When I run your code the audio output is only audible in the left channel as you have specified.
#Austin Kootz' version with the sound()-function is just as good and also produces what you're looking for, but with audioplayer-object you have the ability to stop the playback in the middle of the playback (as you probably know)
Have you tried converting your .wav to another format to see if that makes a change?
I am trying to put together an animation in matlab. For this i am showing pictures containing a description of whats currently happening in the animation. So i am writing out pictures of my figures and later on put these together to an avi file again using matlab. For the description parts to "show up" long enough i use a simple loop in which the current figure is saved n-times. Unfortunatelly this process, though matlab does not have to calculate anything new, is the slowest one.
As the loop i use the following (h_f being the figure handle):
for delay = 1:80
export_fig (h_f,['-r' num2str(resolution)], ['C:\Users\Folder\Name_', '_' num2str(7700+delay) '.png'])
end
I just want to ask if there is any faster way. right now it kind of feels like matlab replots the fig each time before exporting it. So is there a way of plotting the figure once and simultaneously export it n-times?
Thanks for the help :)
If they are all really identical, then you can just copy the first file into the subsequent files. Your time will be limited by the speed of your disk drive and the size of your image files.
% Define some of the flags as variables. This'll make it clearer
% what you're doing when you revisit the code in a year.
% Flag for the resolution
resFlag = sprintf('-r%u', resolutions);
% Base folder
folder = 'C:\Users\Folder\';
% First file number
startnum = 7701;
% This is the pattern all the filenames will use
nameToken = 'Name_%u.png';
% First filename
firstFile = fullfile(folder, sprintf(nameToken, startnum));
% Export the first file
export_fig(h_f, resFlag, firstFile);
numCopies = 80;
% Copy the file
for delay = 2:numCopies
% Make a new filename
currentFile = fullfile(folder, sprintf(nameToken, startnum + delay));
% Copy the first file into the current filename.
copyfile(firstFile, currentFile);
end
I have some problems with matlab 2015a Win10 X64 16GB Ram.
There is a bunch of images (1280x960x8bit) and I want to load them into a 3d matrix. In theory that matrix should store ~1.2GB for 1001 images.
What I have so far is:
values(:,:,:)= zeros(960, 1280, 1001, 'uint8');
for i = Start:Steps:End
file = strcat(folderStr, filenameStr, num2str(i), '.png');
img = imread(file);
values(:,:,i-Start+1) = img;
end
This code is working for small amount of images but using it for all 1001 images I get "Out of memory" error.
Another problem is the speed.
Reading 50 images and save them takes me ~2s, reading 100 images takes ~48s.
What I thought this method does is allocating the memory and change the "z-elements" of the matrix picture by picture. But obviously it holds more memory than needed to perform that single task.
Is there any method I can store the grey values of a 2d picture sequence to a 3d matrix in matlab without wasting that much time and ressources?
Thank you
The only possibility I can see is that your idexes are bad. But I can only guess because the values of start step and End are not given. If End is 100000000, Start is 1 and step is 100000000, you are only reading 2 images, but you are accessing values(:,:,100000000) thus making the variable incredibly huge. That is, most likely, your problem.
To solve this, create a new variable:
imagenames=Start:Step:End; %note that using End as a variable sucks, better ending
for ii=1:numel(imagenames);
file = strcat(folderStr, filenameStr, num2str(imagenames(ii)), '.png');
img = imread(file);
values(:,:,ii) = img;
end
As Shai suggests, have a look to fullfile for better filename accessing
I'm trying to create a movie in Matlab where I run through my code several times, and the output of each runthrough is added to the video. However, I keep getting a strange error when I actually try to add the frame to the movie. Here's a snippet of my code:
writerobj = VideoWriter('RHI_P.avi');
writerobj.FrameRate = 60;
open(writerobj);
\\ Runs through code and generates a pcolor figure
figure(14);
\\ Plotting script goes here....
fig = figure;
F = getframe(fig);
writerobj = writeVideo(writerobj,F);
>> Error using VideoWriter/writeVideo: Too many output arguments.
However, if I look at 'help writeVideo', it says this: "writeVideo(OBJ,FRAME) writes a FRAME to the video file associated with OBJ."
From my point of view (though I could be wrong!), I'm following the example given. I've been stuck on this for a couple days now. Does anyone have any advice?
Also, I've been using getframe(gcf) as a workaround for now. However, I can't keep doing that, because my code takes several days to run, and gcf captures the screensaver once that kicks on.
writeVideo does not have an output argument which is what the error states.
You just need to say
writeVideo(writerObj, F);
Hope this helps.
I'm trying to extract information from a sound file in order to use it in a video classification algorithm I'm working on.
My problem is that I don't know how to work exactly with audio files in Matlab.
Below is what I need to accomplish:
open the audio file and get the sampling rate/frequency
I need to work on a window of 2 seconds so I have to loop over the file and get each 2 seconds as a window and then do the ftt (Fast-Fourier-Transform) on each window.
After that it is my turn to use these values to do what I want
any help would be appreciated
Thanks.
Following code may only give you some idea. You may need to determine another fft size, a windowing function like hamming etc.
To read a wav file:
[data, Fs] = wavread('path.wav');
Splitting to 2 sec windows and getting fft:
frameFFT = [];
timeStep = Fs*2;
for i=1:timeStep:length(data)-timeStep
frameFFT = [frameFFT; fft(data(i:i+timeStep-1),1024)];
end