{} and () brackets - creating array in for loop

Hi everyone. I read the following answer on stack exchange:
Why this code work:
t=[0:0.01:5];
a=[0:1:5];
n=length(a);
for i=1:n
plot(t,t.^a(i));
M(i)=getframe(gcf);
end
while this does not work:
t=[0:0.1:10];
for i=1:5
plot(t,t.^i)
f=getframe(gcf);
immagine(i)=frame2im(f);
end
but immagine{i}=frame2im(f); works?
I don't understand how use () and {}. In M(i) the () brackets work, while in immagine(i) don't work.
Thank you very much for your help.

1 comentario

Jan
Jan el 27 de Jun. de 2017
"Works" and "doesn't work" are lean descriptions. Post the error message instead. Note that it contains important information.

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 27 de Jun. de 2017
It depends on how the variable is defined before the loop:
clear('M'); % Now M is not defined and the first access determines the type
t = 0:0.01:5;
a = 0:1:5;
for i = 1:length(a)
plot(t,t.^a(i));
M(i) = getframe(gcf);
end
You could initialize M by this also:
M = {};
Then the assignment M(i) must fail, but M{i} is correct. Cell arrays are indexed using curly braces, all other arrays use round parenthesis.
If immagine(i) fails, it is immagine was a cell array before. Then:
immagine{i} = frame2im(f);
or
immagine(i) = {frame2im(f)};
works, whereby the first is more efficient: The latter creates a scalar cell array and assigns it.
Please read the documentation about cells: doc cell The Getting Started chapters of the documentation are very useful also.

6 comentarios

Gennaro Arguzzi
Gennaro Arguzzi el 27 de Jun. de 2017
Editada: Gennaro Arguzzi el 28 de Jun. de 2017
The error is:
Subscripted assignment dimension mismatch.
Error in Untitled (line 7)
immagine(i)=frame2im(f);
the whole code is:
close all
clear all
t=[0:0.1:10];
for i=1:5
plot(t,t.^i)
f=getframe(gcf);
immagine(i)=frame2im(f);
end
per isakson
per isakson el 27 de Jun. de 2017
Editada: per isakson el 27 de Jun. de 2017
The error message says () and your code {}. Thus it's not the code you show that throws the error.
"I don't understand how use () and {}" The answer by Andrey Rubshtein at Stackoverflow is good.
Gennaro Arguzzi
Gennaro Arguzzi el 28 de Jun. de 2017
Editada: Gennaro Arguzzi el 28 de Jun. de 2017
I'm sorry @perisakson, I correct the code (the error referrint to (), not to {}).
Jan
Jan el 28 de Jun. de 2017
Editada: Jan el 28 de Jun. de 2017
@Gennaro Arguzzi: If immagine has not been created before, Matlab assumes, it is a double arrays. But then it tries to assign the image replied by frame2im to one element. This must fail. With the curly braces, each image is stored as a cell element.
This would work also:
immagine = uint8([]);
t = 0:0.1:10; % No square brackets
for i=1:5
plot(t,t.^i)
f = getframe(gcf);
immagine(:, :, :, i) = frame2im(f);
end
In the case of:
M(i) = getframe(gcf);
why does the code work?
clear all
close all
t=[0:0.01:5];
a=[0:1:5];
n=length(a);
for i=1:n
plot(t,t.^a(i));
M(i)=getframe(gcf);
end
Maybe this behaviour is related to the natur of the output of getframe and frame2im? In the right case, why?
Jan
Jan el 29 de Jun. de 2017
While
immagine(i)=frame2im(f)
tries to assign an array to a scalar,
M(i)=getframe(gcf)
assigns a scalar struct to a scalar. Try this:
clear('A', 'B', 'C')
A(1) = 15 % Now A is a double
tmp.CData = rand(640, 480, 3);
tmp.Map = [];
B(1) = tmp; % Now B is a struct
C(1) = [15, 16] % Error: cannot assign vector to scalar
Clear now?
Again: It is a good programming practice to avoid unnecessary square brackets.

Iniciar sesión para comentar.

Preguntada:

el 27 de Jun. de 2017

Comentada:

Jan
el 29 de Jun. de 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by