Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 9

IT

Workshop
LECTURE
PROF. SAYAK PRAMANIK
Break
limit = 0.8;

s = 0;

while 1

tmp = rand;

if tmp > limit

break

end

s = s + tmp;

end
Continue
for n = 1:50

if mod(n,7)

continue

end

disp(['Divisible by 7: ' num2str(n)])

end
Example: Skip to next loop
fid = fopen('magic.m','r’); end

count = 0; count = count + 1;

while ~feof(fid) end

line = fgetl(fid);

if isempty(line) || strncmp(line,'%',1) || fclose(fid);


~ischar(line)

continue
Return (to keyboard)
function idx = for idx = 1:length(arrayToSearch)
findSqrRootIndex(target,arrayToSearch) if arrayToSearch(idx) == sqrt(target)

return

end
idx = NaN;
end
if target < 0
end
return
A = [3 7 28 14 42 9 0];
end b = 81;
NOTE: Create a function file
findSqrRootIndex(b,A)
Return (to invoking function)
function returnControlExample(target) else
arrayToSearch = [3 7 28 14 42 9 0]; disp(['Square root found at index '
num2str(idx)])
idx =
findSqrRootIndex(target,arrayToSearch); end

end
if isnan(idx)

disp('Square root not found.') returnControlExample(49)


NOTE: Create a function file
Exceptions & Errors (try catch block)
try try

statements var1

catch exception catch ME

disp(['ID: ' ME.identifier])


statements
disp ('You have entered wrong variable/function
end
name ')

end
Rethrow (rethrow previously caught exception)
try try

var1 var1

catch ME catch ME

disp(['ID: ' ME.identifier])


disp(['ID: ' ME.identifier])
disp ('You have entered wrong variable/function
rethrow(ME)
name ')
end rethrow(ME)

end
Example
A = rand(3); msg = ['Dimension mismatch occurred: First
B = ones(5); argument has ', ...
C = [A; B]; num2str(size(A,2)),' columns while second has ', ...
---------------------------------------------------
num2str(size(B,2)),' columns.'];
A = rand(3);
causeException =
B = ones(5); MException('MATLAB:myCode:dimensions',msg);
try
ME = addCause(ME,causeException);
C = [A; B];
end
catch ME

if rethrow(ME)
(strcmp(ME.identifier,'MATLAB:catenate:dimensionMismatc
end
h'))

You might also like