Issue with VideoWriter and writeVideo in Matlab - matlab

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.

Related

(Matlab) Option to turn pause on and off from output callback of system(string)?

For an aerospace course aerelasticity I am doing an assignment with Nastran in Matlab (by using system(command) and bdf as input file).
I have attached a piece of my code as explanation. In this case the program Nastran produces a punch file (text) with displacements.
Currently the problem is that Matlab disregards the time Nastran needs for analysis to produce this punch file and continues on with the loop, however this punch file is not created yet so matlab turns out an error saying it does not exist and stops the loop.
I "have" a workaround for this by setting the pause times manually found from running it manually for increasing mesh sizes, this gives me at least some data on mesh convergence, however this is not a suitable method to use the rest of the assignment as it will take way too much time, therefore it must be automated.
I was thinking of setting a condition temporarily pausing the loop if the punch file does not exist and turning on again if it exists, however I got stuck with using a pause condition inside a while loop alltogether, it does not seem a solution to me.
Do you have any suggestions / ideas on what I could use / do how to get around this problem
or
know if there is a way to sent a callback from system(nastran) which i can use to create a condition to control the loop or something in that direction?
The following is a piece of code of the created function which turns out the Residual Mean squared error of the mesh which I use to see if the mesh converges:
%% Run Nastran
system('"C:\Users\$$$$\AppData\Roaming\MSC.Software\MSC_Nastran_Student_Edition\2014\Nastran\bin\nastranw.exe" nastranfile.bdf mem=1gb'); % Run the created bdf file
pause(15);
%% Read results and save all relevant results
fpc = fopen('nastranfile.pch','r')
line = 0;
for j=1:6;
line = line+1;
fgets(fpc);
end
line;
counter=0;
data = [];
while ~feof(fpc)
counter= counter+1;
str = fgets(fpc);
line=line+1;
str = str(61:73);
data(counter) = str2num(str)
fgets(fpc);
line=line+1;
end
line;
fclose(fpc);
% Find RMSE
mdl = fitlm(1:length(data),data);
RMEA = mdl.Rsquared.Adjusted;
RMSE = mdl.RMSE;

Problems with plotting in Matlab

Hey, I've got a problem plotting a function in Matlab.
I first run this:
format long
f = inline('-x.^2');
for i = 0:10
[I(i+1) h(i+1) tid(i+1)] = trapets(f,0,1,2^i);
end
trunk = I - log(2);
hold on
grid on
plot(log(h),log(trunk),'r+')
t = -7:0;
c = polyfit(log(h),log(trunk),1);
yy = polyval(c,t);
plot(t,yy)
grid off
hold off
koefficienter = real(c)
and after that I run this file:
hold on
plot(h,trunk,'r+:','linewidth',2)
axis([0 0.6 0 0.0014])
Thing is, I don't get any errors, and the plot windows pops up with axes and all, but there is no graph to be found. It's just an empty window with two axes.
Anyone got any ideas?
Edit:
Okay, so I'm new to this site and couldn't find the reply button, so I add a reply here instead.
#woodchips :
I just realized that I hadn't given you all the information for this problem.. Sorry about that, anyhow I would really appreciate it if someone had the time to help me with this, it would seriously make my week.
This is the part I accidentally left out:
function [ I,h,tid ] = trapets(
f,a,b,n )
h=(b-a)/n;
tic; I=(f(a)+f(b));
for k=2:2:n-2
I = I+2*f(a+k*h);
end
for k = 1:2:n-1
I = I + 4*f(a+k*h);
end
I = I * h/3;
tid = toc;
end
Edit 2: So, I think that the graph I'm seeking is actually getting plotted in the first code that I wrote, the problem is that the variabe 'I' is not changing, which I expect it to do, although the variabels 'n' and 'h' do change. If 'I' was working correctly, I would probably get the right graph (hopefully). Any ideas, anyone?
Unfortunately the home computer I had with Matlab on it died the other day so I can't test anything. First thing I can think of if to simply run step by step through the code and see if the results of the math are what you are expecting. For instance Matlab was primarily made and runs as a matrix calculator if I recall correctly. As such most of the simple math doesn't function as it would punching it in a calculator. An example would be that 2^i needs to be 2.^i to operate correctly in some cases. Same with .* and ./ to use the singular scalar verses the matrices math.
The best way to find out what is going wrong is to iterate through the math a few times to ensure that it is being performed as expected. Once that is verified then you can move on to looking at plotting formatting.

How to make an animation from multiple results?

I have some calculation with matrices and have set my loop to run for (let's say) 50 times.
I also assigned a color to each value so I can get a picture in the end of these matrices based on their values.
What I don't know is - how to make an animation from this multiple images I get in each turn.
Is it possible?!
The following code is what I have used previously to produce an .avi file
n = 15;
p = randperm(n);
figure('Color','white');
fcount = 0;
for k = 1:n-1
% produce the plot
[idx,idx] = min(p(k:n));
p(idx+k-1) = p(k);
p(k) = k;
plot(p,'*')
% Make sure plot updates before we capture the contents
pause(0.1)
F(k) = getframe(gcf); %#ok
end
movie2avi(F,'so1.avi','fps',2,'quality',100);
However, there seems to be some issues with the avi codec now for use with Windows XP, for example see this thread.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/271172
I had the same problem; the avi file produced with the default Indeo codec would not run in Windows Media Player. Using a different codec, such as
movie2avi(F,'so1.avi','fps',2,'quality',100,'compression','Cinepak');
solved the problem. You may need to experiment to find a working combination.
Hth, Darren
I'm not sure what you're trying to do. One option is to use the MS-GIF animator, although 50 images is a bit much. See http://en.wikipedia.org/wiki/Microsoft_GIF_Animator for info. Considering the number of images, you might want to create a powerpoint document.

How can I get a waitbar to work in Matlab?

I want to have a waitbar for an operation that takes quite a while. Here is my code:
h = waitbar(0,'Please wait...');
for i=1:counterend
waitbar(i/waitbarcounter)
Atemp = At+i*step;
handle = #(M) 1/M^2*((2/(gamma+1))*(1+(gamma-1)*M^2/2))^((gamma+1)/(gamma-1))-(Atemp/At)^2;
Mach = fzero(handle, 5);
Aplot(i) = Atemp/At;
Tplot(i) = Tc / (1+(gamma-1)*Mach^2/2);
Mplot(i) = Mach;
plot(Aplot, Tplot)
end
close(h)
The error Matlab gives is:
??? Error using ==> waitbar at 249
Improper arguments for waitbar
After investigation, I am sure that this error must occur because of the sorrounding code in the loop.
Note: The loop works fine without the waitbar.
Running
counterend = 10000;
>> h = waitbar(0,'Please wait...');
for i=1:counterend
waitbar(i/counterend)
end
close(h);
Works as expected on 2007a / Windows XP.
On a side note, it would help knowing what countered is defined as. Something quick to check would be to ensure that you are not passing it a CELL.
Running
counterend = {10000};
h = waitbar(0,'Please wait...');
for i=1:counterend
waitbar(i/counterend)
end
close(h);
Yields a different error (see below) in 2007a, but this error message may have changed in 2008.
??? Undefined function or method
'_colonobj' for input arguments of
type 'cell'.
My last bit of advice would be caution you on the use of waitbar for large arrays/data sets. While I think it is important to inform the user of the progress, to me there is also a concern for how much time is added to the loop. Working with arrays that have 100k+ entries, I became a religious user of the Profiler to see where the time was really being spent. I can tell you the time is not in the calculation of the i/X, it was all in updating the waitbar's display. To soften the blow of the update/drawnow, I only updated the waitbar every 100 to 1000 entry which helped tremendously.
EDIT: Updated response to match latest code
I first started to attack this problem at the anonymous function, having problems with them in the past it's a personal vendetta of mine. When looking into the function I found that you are using gamma, do you have this defined as a constant (constant to the loop / function)? The reason I ask is that 'gamma' is a Matlab function and was giving me errors when trying to run your function by itself. Below I have modified you code slightly and this does run fine here. If any of the assumptions I have made are wrong please let me know.
In addition, if you did intend to use the gamma function, your function is missing any arguments to it. Hope this helps!
clc
h = waitbar(0,'Please wait...');
counterend = 1000;
waitbarcounter = counterend;
g_amma = 7;
At = 34;
step = 2;
Tc = 42;
for i=1:counterend
waitbar(i/waitbarcounter)
Atemp = At+i*step;
handle = #(M) 1/M^2*((2/(g_amma+1))*(1+(g_amma-1)*M^2/2))^((g_amma+1)/(g_amma-1))-(Atemp/At)^2;
Mach = fzero(handle, 5);
Aplot(i) = Atemp/At;
Tplot(i) = Tc / (1+(g_amma-1)*Mach^2/2);
Mplot(i) = Mach;
plot(Aplot, Tplot)
end
close(h)
Regards,
Adam
I have checked waitbar on R2008b. So far, the only ways I was able to reproduce your error was by having i/counterend evaluate to an array with multiple rows (a 1x2 vector gives interesting results), and by closing the waitbar before calling waitbar(i/counterend).
I do not get any error running the following:
h = waitbar(0,'Please wait...');
counterend = 1000;
for i=1:counterend
waitbar(i/counterend)
end
close(h)
Could you make sure that the small example above runs without error? If yes, please check that the waitbar is not closed during the execution of the loop, and that counterend is a scalar (use dbstop if error to stop execution of your code at the time of the error).
If the above example does not work without error, you should use which waitbar to check that you are using Matlab's waitbar, and not any of the many updated version from the Matlab File Exchange.
Running
counterned=1000;
h = waitbar(0,'Please wait...');
for i=1:counterend
waitbar(i/counterend)
end
close(h)
works perfectly as expected on MATLAB R2009a on Windows XP.
The above runs fine on R2008a on XP also.
However, you get the error you describe if you kill the waitbar window before the next waitbar command comes around. If you want to be nice about it you should check if the handle h is still valid before issuing waitbar.
I prefer to use progressbar written by Steve Hoelzer on the MATLAB FEX. I haven't had any problems with it.
Your suppose to use the handle that you created with your first line of code when you want to update the waiter,
Waiter(it/itmax,h,'progress')

Failed to write stream data, matlab

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.