简体   繁体   中英

How to convert try-catch statements in octave to a Matlab form?

I need to convert an old Octave code to Matlab R2017a form. Matlab is not understanding the to-be-mentioned code and it is giving the error :

Undefined variable "lasterror" or class "lasterror.message".

Error in computeCentroids (line 16) msg = lasterror.message;

Mathworks are saying that "lasterror" is going to be—or already has been— deleted from more modern versions of Matlab. I personally am not familiar with lasterror.message nor with the try-catch pair. I Hope you help me.

code:

function centroids = computeCentroids(Xnum, Xcat, idx, K) 
[m n] = size([Xcat Xnum]);
centroids = zeros(K, n);
for i=1:K,
    indx = (idx(:,1)== i);
    try
        centroids(i,:) = [findMode(Xcat, indx) computeMean(Xnum, indx)];
    catch
        msg = lasterror.message;
        fprintf(strcat(msg, '\n'));
    end_try_catch
    end
end
.
.
.
.
end

Please check the documentation . You can print the error like this:

try
   %Error-maker
catch e 
    fprintf(1,'There was an error! The message was:\n%s',e.message);
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM