Error in MATLAB executing max () function: Subscript index must be a positive integer type or logical type

Source: Internet
Author: User
Error in MATLAB executing max () function: Subscript index must be a positive integer type or logical type

An error occurred when using the Max () function of Matlab to find the maximum value for a column of double type data: The subscript index must be a positive integer type or a logical type. Here is a partial matlab code :

    Frid = fopen (infile (i). Name, ' RT ');
    Fwid = fopen (outfile, ' A + ');
    A = Textscan (Frid, '%d%d%d%f ');
    B = a{4};
    Maxval = max (B);

I couldn't understand the reason for the mistake. At first I thought B was a problem, but when I looked at the MATLAB reference manual, I found it could be used. The subsequent use of clear clears the variable when the execution of similar code is successful, so I think it should be a variable problem.

The careful classmate may notice that the last line left the variable max and the right function Max have the same name. Yes, that's why the mistake happened. There is no Max variable for the first execution, so the max () function can execute correctly. With the first execution of the max variable generation, when the max () function is executed again, instead of the max () function that comes with MATLAB, the first generation of the variable max is used instead. And the variable max is not on this side as a function, but as an array, so the right side should be the subscript index (positive integer type or logical type), but B is not such a type. Error: The subscript index must be a positive integer type or a logical type.

The workaround is simple, as long as the variable max before the error line equals sign is changed to a variable name that is not the same as the function Max (), such as Maxval.

I first contact with Matlab, so will make some relatively low-level errors. Record it down to remind myself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.