How to clear CurrentCharacter
    6 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
I am trying to cycle through a few different figures within the same figure window. I have a few while and if loops with counters and flags. I current waits for the first key press to switch to the next figure, then after a key has been pressed, it just cycles through the figures forever. I stepped through my code and believe the issue is due to the CurrentCharacter object/veriable not resting after a key is pressed.
I tried setting it to zero, but after the first keypress, it always contains something, causing isempty to always return 1, thus causing the loop to switch to the other figure.
Z = 1;
ZZ = 1;
forever = 1; 
while forever
     while Z == 1
         if ZZ == 1
            %figure('units','normalized','outerposition',[0 0 1 1]);
            figure(10),clf;
            hold on;
            h = figure(10);  
            plot(waveform.XData,waveform.YData);
            set(gca,'XTick',(min(waveform.XData):waveform.SecPerDiv:max(waveform.XData)))
            hold off;
            drawnow
            ZZ = 0;
        end
        pause(0.1);  
        isKeyPressed = ~isempty(get(h,'CurrentCharacter'));
        if isKeyPressed
            Z = Z+1;
            ZZ = 1;
            isKeyPressed = 0;
            CurrentCharacter = '';
        end           
    end
    while Z == 2
        if ZZ == 1
            %figure('units','normalized','outerposition',[0 0 1 1]);
            figure(10),clf;
            hold on;
            plot(H,AllFFT_mean,'DisplayName','Oscilloscope Measured Signal'); hold on;
            stem(X2,triH,':^m','BaseValue',-140); hold on;
            stem(X2,sqrH,':sr','BaseValue',-140); hold on;
            grid minor;
            xlim([0 2020]);
            hold off;
            drawnow
            ZZ = 0;
        end
        isKeyPressed = ~isempty(get(h,'CurrentCharacter'));
        if isKeyPressed
            Z = Z-1;
            ZZ = 1;
            CurrentCharacter = '';
        end     
    end
end 
I need to set [get(h,'CurrentCharacter')] to nothing.
I tried set(h,'CurrentCharacter',' ') to manually change the object properties. This does allow me to change the value in the keypress array but does not allow me to set it to '' or clear it.
I'm trying to have my code switch between multiple different figures with a single switch from a single keypress. Anyother methods to achive this would help.
Thanks
0 Kommentare
Antworten (1)
  Mohith Kulkarni
    
 am 5 Okt. 2020
        You can use a temporary figure to check for key press. Refer to the code below for a workaround
if Z == 1    
    if ZZ == 1
        tempf = figure('Visible','off');
        loop = true;
        while loop
            figure(tempf);
            pause % wait for a keypress
            isKeyPressed = ~isempty(get(tempf,'CurrentCharacter'));
            if isKeyPressed
                loop = false;
                Z = Z+1;
                ZZ = 1;
            end
        end
        delete(tempf);    
    end
end
Also, do notice that i have taken the while loop to check for key press inside the if condition, instead of while Z == 1 i have used if Z == 1 and pushed the while loop inside.
forever = 1; 
XData = [1 2 3 4];
YData = [3 5 7 9];
XData2 = [5 6 7 8];
YData2 = [3 5 7 9];
h = figure(10);
data.A = [1,1];
%data.ZZ = 1;
guidata(h,data);
set(h,'KeyPressFcn', {@key_pressed_fcn});
while forever
     if data.A(1) == 1
         if data.A(2) == 1
            %figure('units','normalized','outerposition',[0 0 1 1]);
            figure(10),clf;
            hold on;
            h = figure(10);  
            plot(XData,YData);
            %set(gca,'XTick',(minXData):waveform.SecPerDiv:max(waveform.XData)))
            hold off;
            drawnow
            data.A(2) = 0;
            guidata(h,data);
         end
         while data.A(1) == 1
           data = guidata(h);
           pause(0.5);
         end
     end
     if data.A(1) == 2
        if data.A(2) == 1
            %figure('units','normalized','outerposition',[0 0 1 1]);
            figure(10),clf;
            hold on;
            %plot(H,AllFFT_mean,'DisplayName','Oscilloscope Measured Signal'); hold on;
            plot(XData2,YData2);
            %stem(X2,triH,':^m','BaseValue',-140); hold on;
            %stem(X2,sqrH,':sr','BaseValue',-140); hold on;
            grid minor;
            %xlim([0 2020]);
            hold off;
            drawnow
            data.A(2) = 0;
            guidata(h,data);
        end
        while data.A(1) == 2
            data = guidata(h);
            pause(0.5)
        end   
     end 
end 
set(h,'KeyPressFcn', 'remove');
function key_pressed_fcn(h,~)
    disp('pressed')
    data = guidata(h);
    if data.A(1) == 1
        disp('Z=1')
        data.A(1) = data.A(1)+1;
    elseif data.A(1) == 2
        disp('Z=2')
        data.A(1) = data.A(1)-1;
    end
    data.A(2) = 1;
    guidata(h,data);
end
Make sure to revert back the minor changes I made because I do not have the data for plotting, i used dummy data instead.
Siehe auch
Kategorien
				Mehr zu Graphics Performance finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

