how to replace the variable value permanently

3 ビュー (過去 30 日間)
Matuno
Matuno 2013 年 12 月 18 日
編集済み: ES 2013 年 12 月 19 日
I am trying to update a variable value permanently. I want to make a GUI where there will be a button which would have variable's value (e.g. k = 3), I will add that with 4 and show the value (e.g. 7) in a text box. Now the value (i.e. 7) will be the new value of k. So if again I click on the button it would show the updated value in text box (e.g. k=7, in text box: 7+4 =11). I am new in matlab and tried many ways to solve it. The simplest way was:
function addition_Callback(hObject, eventdata, handles)
k =3;
k = 4+k;
set(handles.value,'String', ... %here value is the name of the text box
[ k ]);
but each time I am clicking the button, it is starting from very beginning as assumed. How can I declare the variable so that it will work the way I just mentioned?

回答 (2 件)

ES
ES 2013 年 12 月 18 日
you can read what is displayed in the text box, add 4 to it and display the new result. like this,
function addition_Callback(hObject, eventdata, handles)
k =get(handles.textbox,'string');
k=str2double(k);
k = 4+k;
set(handles.textbox,'String', ... %here value is the name of the text box
[ k ]);
  2 件のコメント
Matuno
Matuno 2013 年 12 月 18 日
Thank you for your answer. But I need to initialize the k =3 at first. I mean the k's value has to be 3 for the very first time. Could you please show me a way to do that? Thank you again for your time.
ES
ES 2013 年 12 月 19 日
編集済み: ES 2013 年 12 月 19 日
the default value of K be set to 3. What I mean is, in the Property editor of your text box, set the string to 3. So when your gui displays, it will have 3. For every press of the pushbutton, addition call back will be called and the script will do as you want.

サインインしてコメントする。


ES
ES 2013 年 12 月 18 日
or simply, you can set k as global value or persistent.
global k; or persistent k;
eg:
function addition_Callback(hObject, eventdata, handles)
persistent k;
k = 4+k;
set(handles.textbox,'String', ... %here value is the name of the text box
[ k ]);

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by