How to skip an error inside a loop so the loop continues?

17 Ansichten (letzte 30 Tage)
sittmo
sittmo am 23 Feb. 2014
Kommentiert: Image Analyst am 25 Okt. 2016
The following is my full code: (The part of the code that is causing me trouble is towards the end)
clc;
clear;
P = xlsread('b3.xlsx', 'P');
d = xlsread('b3.xlsx', 'd');
CM = xlsread('b3.xlsx', 'Cov');
Original_PD = P; %Store original PD
LM_rows = size(P,1)+1; %Expected LM rows
LM_columns = size(P,2); %Expected LM columns
LM_FINAL = zeros(LM_rows,LM_columns); %Dimensions of LM_FINAL
% Start of the outside loop
for k = 1:size(P,2)
P = Original_PD(:,k);
interval = cell(size(P,1)+2,1);
for i = 1:size(P,1)
interval{i,1} = NaN(size(P,1),2);
interval{i,1}(:,1) = -Inf;
interval{i,1}(:,2) = d;
interval{i,1}(i,1) = d(i,1);
interval{i,1}(i,2) = Inf;
end
interval{i+1,1} = [-Inf*ones(size(P,1),1) d];
interval{i+2,1} = [d Inf*ones(size(P,1),1)];
c = NaN(size(interval,1),1);
for i = 1:size(c,1)
c(i,1) = mvncdf(interval{i,1}(:,1),interval{i,1}(:,2),0,CM);
end
c0 = c(size(P,1)+1,1);
f = c(size(P,1)+2,1);
c = c(1:size(P,1),:);
b0 = exp(1);
b = exp(1)*P;
syms x;
eqn = f*x;
for i = 1:size(P,1)
eqn = eqn*(c0/c(i,1)*x + (b(i,1)-b0)/c(i,1));
end
eqn = c0*x^(size(P,1)+1) + eqn - b0*x^size(P,1);
x0 = solve(eqn);
for i = 1:size(x0)
id(i,1) = isreal(x0(i,1));
end
x0 = x0(id,:);
x0 = x0(x0 > 0,:);
clear x;
for i = 1:size(P,1)
x(i,:) = (b(i,1) - b0)./(c(i,1)*x0) + c0/c(i,1);
end
x = [x0'; x];
x = double(x);
x = x(:,sum(x <= 0,1) == 0)
lamda = -log(x);
LM_FINAL(:,k) = lamda;
end
% end of the outside loop
The important part of the above loop is towards the end:
x = x(:,sum(x <= 0,1) == 0)
This condition is sometimes not satisfied and hence the variable x is empty, which means LM_FINAL(:,k) = lamda is also empty. When this happens, I get the error:
x =
Empty matrix: 43-by-0
Improper assignment with rectangular empty matrix.
Error in Solution (line 75)
LM_FINAL(:,k) = lamda;
How can I skip this error so that the column for LM_FINAL remains as empty, but the loop continues (so that the rest of LM_FINAL's columns are filled) rather than terminating?

Akzeptierte Antwort

the cyclist
the cyclist am 23 Feb. 2014
You could wrap the code inside the line
if not(isempty(x))
...
end
  2 Kommentare
sittmo
sittmo am 23 Feb. 2014
Bearbeitet: sittmo am 23 Feb. 2014
Thanks, so just to confirm, this leaves the column of LM_FINAL as a column of 0's (i.e., unchanged) if the error happens and then proceeds to fill in the next column (assuming the error doesn't occur)?
the cyclist
the cyclist am 23 Feb. 2014
Yes, that is what should happen. You have initialized the entire array to be zeros, which of course means that each column starts off as zero. If you do
if not(isempty(x))
lamda = -log(x);
LM_FINAL(:,k) = lamda;
end
then it is only going to run that code if x is not empty. If x is empty, then it is going to do nothing with LM_FINAL, which means that that column will continue to be all zeros.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 23 Feb. 2014
You could wrap the known troublesome code in try catch:
try
% code that generates an error
catch
% Error goes here but don't do anything, just ignore it.
end
  6 Kommentare
Dean Ranmar
Dean Ranmar am 25 Okt. 2016
I have to aplogize and this is a perfect example of why it's necessary to include the actual code you're using when asking a question. I cannot do as you suggest because the software that creates these csv files includes a header line before each array (matrix) which is in hex format. csvread expects only integer or floating point numbers so, if I read these lines - including the case where I try to read the entire file, execution is terminated. I cannot directly control the format in which these files are created but, I will try to persuade the provider to alter them. Thanks and I'm sorry I wasted your time; your solution would obviously work, otherwise.
Image Analyst
Image Analyst am 25 Okt. 2016
You could use importdata() which allows you to specify the number of headerlines. Alternatively you could preprocess the file to extract out each chunk and write each chunk to a separate file. Of course it's easier if you can get them to just supply you with individual files in the first place.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by