コールバック関数から入力された以前の値を取得するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/2939285

質問

これはおそらく単純な問題であることは知っていますが、私はMatlab GUIの初心者であり、基本的に、入力されたばかりの値を置き換えるためにテキストボックスに保存されていた古い値を取得したいと考えています。例えば

  1. テキストボックスには有効な文字列が含まれています。
  2. ユーザーは無効な文字列に入り、
  3. コールバックFUNC、入力を検証し、新しい入力がエラーであることを実現し、古い以前の値に戻ります。

これはどのように実装または実行する必要がありますか? ATM私はGETおよびSETプロパティ値を使用しています。以下はいくつかのサンプルコードです。

function sampledist_Callback(hObject, eventdata, handles)
% hObject    handle to sampledist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of sampledist as text
%        str2double(get(hObject,'String')) returns contents of sampledist as a double

input = str2double(get(hObject,'String'));
if(input < 0 || input > 500)
    errordlg('Sampled Dist. must be > 0 and < 500','Sample Dist - Input Error');
    set(handles.sampledist,'String',['10']); %<--- I would like this value 10 to be the previous entry!
    guidata(hObject,handles);
else
   set(handles.sampledist,'String',['',input]);
   guidata(hObject,handles);
end
役に立ちましたか?

解決

新しいフィールドを追加するだけです sampledistPrev ハンドル構造に。

の中に openingFcn GUIの場合、このような行でプロパティを定義します。

handles.sampledistPrev = 10; %# or whatever you choose as default value
%# if you want, you can set the default value to the GUI, so that you only need 
%# to change it at one point, if necessary, like so:
set(handles.sampledist,'String',num2str(handles.sampledistPrev));
%# don't forget to save the handles structure at the end of the openingFcn
guidata(hObject,handles)

次に、このようにコールバックを更新します。

function sampledist_Callback(hObject, eventdata, handles)
% hObject    handle to sampledist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of sampledist as text
%        str2double(get(hObject,'String')) returns contents of sampledist as a double

input = str2double(get(hObject,'String'));
if(input < 0 || input > 500)
    errordlg('Sampled Dist. must be > 0 and < 500','Sample Dist - Input Error');
    set(handles.sampledist,'String',num2str(handles.sampledistPrev)); %reset value be the previous entry!
    guidata(hObject,handles); %# Note that you don't need to save the handles structure unless
                              %# you have changed a user-defined value like sampledistPrev
                              %# It may still be useful to do it so you always remember
else
   set(handles.sampledist,'String',['',input]);
   %# also update the reset value
   handles.sampledistPrev = input;
   guidata(hObject,handles);
end

他のヒント

次のように、「以前の値」をそのオブジェクトの「userdata」として保存してみませんか。


function sampledist_Callback(hObject, eventdata, handles)
    input = str2double(get(hObject,'String'));
    if (input < 0 || input > 500)
        errordlg('Sampled Dist. must be > 0 and < 500','Sample Dist - Input Error');
        val=get(hObject,'UserData');
        if isempty(val)
            val='';
        end
        set(hObject,'String',val); %<--- This is where you'd like to set the previous entry value!
        guidata(hObject,handles);
    else
        input=num2str(input);
        set(handles.sampledist,'String',input,'UserData',input);
        guidata(hObject,handles);
    end
end

%yt

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top