How to fix the error in the color_post function in this code?

31 Views Asked by At

I have this data:

lon = [121,1.212500000000000e+02,1.215000000000000e+02,1.217500000000000e+02,122];
lat = [10,10.250000000000000,10.500000000000000,10.750000000000000,11];
bdlvl = [0,1,2,3,4];

I want to use the function color_post for this data. However, I am encountering some errors. For instance when I input color_post(lon, lat, bdlvl, []) it sends back an error indicating "Index in position 1 exceeds array bounds. Index must not exceed 1".

Also there is an error with this certain line:

item=floor((1+var(ibb2(i),1)-a)/(b-a)*63);

Here's the code:

function color_post(lon,lat,var,limit)
    % mapping z values with gradient of colors
    % make sure you have set your projection area already
    m=length(var);
    hold on 
    
    load('C:\Users\Pia\Downloads\scripts\MyRainbowCmap.mat')
    cmap=mycmap;
    
    if isempty(limit)==1;
        a = min(var); b = max(var);
    else
        a = limit(1); b=limit(2);
    end
    
    caxis([a b]);
    hold on
    
    var(imag(var) ~= 0) = NaN;
    ibb2 = find(isfinite(var));
    %for i=length(ibb2):-1:1;
    for i=1:length(ibb2);
        item=floor((1+var(ibb2(i),1)-a)/(b-a)*63);
        if(item<1)
            item=1;
        end
        if(item>64)
           item=64;
        end
        m_plot(lon(ibb2(i)),lat(ibb2(i)),'o','color',cmap(item,:),'markerfacecolor',cmap(item,:),'MarkerSize',7);   
    end
    fig=gcf; set(fig,'Colormap',mycmap);
    %h=colorbar;

I tried changing the for i=1:length(ibb2); into for i=0:length(ibb2);. I expect that this function will help me to classify my bed level values according to colors.

Hope you could help me!

1

There are 1 best solutions below

0
Im Groot On

You have to change var(ibb2(i), 1) to var(1, ibb2(i));

In the for loop at item=floor((1+var(ibb2(i),1)-a)/(b-a)*63);, you are accessing the each elements of var but you are iterating over its rows and fixed the column var(ibb2(i), 1).

var has 1 row and 5 columns. You have to iterate over the coulmns while keeping the row constant like this var(1, ibb2(i)). The final equation will be

item=floor((1+var(1, ibb2(i))-a)/(b-a)*63);

Hope that helps