如何永久替换变量值 [英] how to replace the variable value permanently

查看:93
本文介绍了如何永久替换变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Matlab中永久更新变量值.我想制作一个GUI,其中将有一个具有变量值(例如k = 3)的按钮,我将其添加4并在文本框中显示该值(例如7).现在该值(即7)将是k的新值.因此,如果我再次单击该按钮,它将在文本框中显示更新后的值(例如,k = 7,在文本框中:7 + 4 = 11).我是Matlab的新手,并尝试了多种方法来解决它.最简单的方法是:

I am trying to update a variable value permanently in Matlab. 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?

推荐答案

为什么不简单地使用当前显示的字符串作为起点?

Why not simply use the currently displayed string as starting point?

function addition_Callback(hObject, eventdata, handles)
  % get the currently displayed value and convert it to a number
  current = str2double(get(handles.value, 'String'));
  % current will be nan if the string is empty or not a valid number
  if isnan(current)
      current = 3; % start or fallback-value
  end
  new = 4+current;
  set(handles.value,'String', new)   %here value is the name of the text box

这篇关于如何永久替换变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆