Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

switch case in GUI pop-up menu

Asked by Chethan on 28 Mar 2013

I've a pop-up menu with 5,10,15,20 the contents in that menu. using switch I've created this, what changes i need to do with this statement?

    val=get(hObject,'value');
    switch val
        case '5'
            n=5;
        case '10'
            n=10;
        case '15'
            n=15;
        case '20'
            n=20;
    end
    handles.n = val;
    guidata(hObject, handles);

where it represents number of output images. I'm suppose to get 5 images at output if user selects 5 in pop-up menu, 10 images if 10 is selected and so on.... But I'm getting only 1 image if i select 5, 2 images if 10, 3 if 15... like that, what's wrong in my switch statement? any appropriate answer is appreciable.

2 Comments

Vishal Rane on 28 Mar 2013

Can't say anything based on the code you have given. Can you share the code where "n" is actually used ?

Chethan on 28 Mar 2013

well sorry for that, above code is written in popup menu callback function.. where i actually use this 'n' is

    function Search_Callback(hObject, eventdata, handles)
    % hObject    handle to Search (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    n = handles.n;
    isTexture = get(handles.Texturecheck,'Value');
    isColour = get(handles.colourcheck,'Value');
    if and(isTexture, isColour)
            % Colour search...
            % Open database txt file... for reading...
            fid = fopen('database.txt');
            resultValues = [];      % Results matrix...
            resultNames = {};
            i = 1;                  % Indices...
            j = 1;
            while 1
                imagename = fgetl(fid);
                if ~ischar(imagename), break, end      
                [X, RGBmap] = imread(imagename);
                HSVmap = rgb2hsv(RGBmap);
                D = quadratic(handles.queryx, handles.querymap, X,HSVmap);
                resultValues(i) = D;
                resultNames(j) = {imagename};
                i = i + 1;
                j = j + 1;
            end
            fclose(fid);
            % Sorting colour results...
            [sortedValues, index] = sort(resultValues);   
            fid = fopen('colourResults.txt', 'w+');        
            *for i = 1:n                 % Store top n matches...*
            tempstr = char(resultNames(index(i)));
            fprintf(fid, '%s\r', tempstr);
            disp(resultNames(index(i)));
            disp(sortedValues(i));
            disp('  ')
        end

I'm suppose to get 5 images at output if user selects 5 in pop-up menu, 10 images if 10 is selected and so on.... But I'm getting only 1 image if i select 5, 2 images if 10, 3 if 15... like that

Chethan

Products

1 Answer

Answer by Jing on 28 Mar 2013

It looks like you're using the selected index(the value you get is index...) of the popup menu, not the selected content for getting the output. When you use switch, it should be like this:

      switch val
          case 1
              n=5;
          case 2
              n=10;
          case 3
              n=15;
          case 4
              n=20;
      end
      handles.n = n; %supposed handles.n is what you use to get output images.

1 Comment

Chethan on 2 Apr 2013

Well, I'm getting error like Reference to non-existent field 'n'.

To over come this i added handles.n=val;

in opening function, even though I'm not getting exact result.

Jing

Contact us