Question

when i run the following code i get an error message "SWITCH expression must be a scalar or string constant."

note that the variable FUD is an cell array of strings and unique(FUD(i,1)) returns a char. also note that this is a runtime error, so the if statement evaluated as true.

if isscalar(unique(FUD(i,1)))  % evaluates to true, therefore it is scalar
    switch unique(FUD(i,1))    % "hurr durr durr not scalar!" <--- MATLAB
        case 'JAN_RP10 .csv'
            month=1;
            break
        case 'FEB_RP10 .csv'
            month=2;
            break
        case 'MAR_RP10 .csv'
            month=3;
            break
        case 'APR_RP10 .csv'
            month=4;
            break
        case 'MAY_RP10 .csv'
            month=5;
            break
        case 'JUN_RP10 .csv'
            month=6;
            break
        case 'JUL_RP10 .csv'
            month=7;
            break
        case 'AUG_RP10 .csv'
            month=8;
            break
        case 'SEP_RP10 .csv'
            month=9;
            break
        case 'OCT_RP10 .csv'
            month=10;
            break
        case 'NOV_RP10 .csv'
            month=11;
            break
        case 'DEC_RP10 .csv'
            month=12;
            break
        otherwise
            disp('switch error')
            break
    end
end

I dont understand why i get this error. I put the initial if statement in as proof that "unique(FUD(i,1))" is scalar. what is happening here? is there something im missing?

here is what FUD looks like (10 by 3 cell):

'APR_RP10 .csv' -56.5 70

'APR_RP10 .csv' -57 71

'DEC_RP10 .csv' -64 70

'DEC_RP10 .csv' -63.5 70

'DEC_RP10 .csv' -64.5 70.5

'DEC_RP10 .csv' -64 70.5

'DEC_RP10 .csv' -64 75

'FEB_RP10 .csv' -57 54

'FEB_RP10 .csv' -56.5 54.5

'FEB_RP10 .csv' -58.5 55

Thanks

No correct solution

OTHER TIPS

The problem is with the statement:

unique(FUD(i,1))

if you replace it with:

x = 'JAN_RP10 .csv';
switch x;
...

everything works well. So check what FUD(i,1) is and check the unique(FUD(i,1)) for all values of i. Anyways, to say what exactly is happening you should give an example of FUD cell.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top