Use switch for a matrix row

조회 수: 11 (최근 30일)
JamJan
JamJan 2018년 4월 26일
댓글: Birdman 2018년 4월 26일
Hello,
I want to use switch to create a video with videowriter. I have written this piece of code. I want to use the cases to judge the rows which picture it has to insert as a frame, but if I do this, I get the following error:
SWITCH expression must be a scalar or character vector constant.
Error in FootSwitchDataAnalysis (line 120) switch M3(i,:);
How can I fix this?
M3 = zeros(6,4)
for i=1:size(M3,i);
switch M3(i,:)
case M3(i,:) == [0 0 0 0]
Frame = imread('FootVis.png')
case M3(i,:) == [1 1 1 1]
Frame = imread('FootVis3.png')
end
frame2insert = im2frame(Frame)
writeVideo(v,frame2insert)

답변 (1개)

Birdman
Birdman 2018년 4월 26일
편집: Birdman 2018년 4월 26일
Use if instead switch:
if all(M3(i,:)==[0 0 0 0])
if all(M3(i,:)==[1 1 1 1])
  댓글 수: 2
JamJan
JamJan 2018년 4월 26일
Thank you, for your quick answer, however:
I had a 16 elseif statements that I used before, because those cases are only 2 of the 16. That worked fine, but I wanted to use switch instead. This was the code:
for i=1:size(M3,i)
% if M3(i,:) == [0 0 0 0]
% A = imread('FootVis.jpeg');
% elseif M3(i,:) == [1 1 1 1]
% A = imread('FootVis2.png');
% elseif M3(i,:) == [1 1 1 0]
% A = imread('FootVis3.jpg');
% elseif [1 1 0 0]
% A = imread('FootVis.jpeg');
% elseif [1 1 0 1]
% A = imread('FootVis.jpeg');
% elseif [1 0 1 1]
% A = imread('FootVis.jpeg');
% elseif [1 0 1 0]
% A = imread('FootVis.jpeg');
% elseif [1 0 0 1]
% A = imread('FootVis.jpeg');
% elseif [1 0 0 0]
% A = imread('FootVis.jpeg');
% elseif [0 1 1 1]
% A = imread('FootVis.jpeg');
% elseif [0 1 1 0]
% A = imread('FootVis.jpeg');
% elseif [0 1 0 0]
% A = imread('FootVis.jpeg');
% elseif [0 1 0 1]
% A = imread('FootVis.jpeg');
% elseif [0 0 1 1]
% A = imread('FootVis.jpeg');
% elseif [0 0 1 0]
% A = imread('FootVis.jpeg');
% else [0 0 0 1]
% A = imread('FootVis.jpeg');
% end
% frame2insert = im2frame(A)
% writeVideo(v, frame2insert)
Birdman
Birdman 2018년 4월 26일
But as said in the error
SWITCH expression must be a scalar or character vector constant.
Therefore you need to take into consideration what I said. Use ismember for comparison of numeric arrays or use all with the structure that I said.
if all(M3(i,:)==[0 0 0 0])
or
if prod(ismember(M3(i,:),[0 0 0 0]))

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by