string - MATLAB Storing a value which is updated continously -
i have matlab gui (by guide) helps me calculations..
i have 'calculate' push button calculates new weight , x value every time click (and manipulate inputs)
i need store 2 value in matrix form dont know how it. needs create matrix equal length of calculate values , store them..
a loop counts number of clicks 'calculate' can not sure.
any appreciated.
here simple code think want. created 'programmatic' gui purpose of demonstration, it's easy implement in guide code. specifically, @ calculatecallback
, callback executed when user presses pushbutton used 'calculate'. here there no calculation/data manipulation per se, code takes user inputs text boxes , updates matrix values. there counter keep track of number of calculation.
the code commented si should easy follow. if not please ask clarifications!
function calculategui clear clc close %// create figure , uielements handles.fig = figure('position',[440 500 500 150]); handles.calcbutton = uicontrol('style','pushbutton','position',[60 70 80 40],'string','calculate','callback',@calculatecallback); handles.val1text = uicontrol('style','text','position',[150 100 60 20],'string','value 1'); handles.val1edit = uicontrol('style','edit','position',[150 70 60 20],'string',''); handles.val2text = uicontrol('style','text','position',[220 100 60 20],'string','value 2'); handles.val2edit = uicontrol('style','edit','position',[220 70 60 20],'string',''); handles.matrixtext = uicontrol('style','text','position',[290 125 100 20],'string','value 1 value 2'); handles.matrixedit = uicontrol('style','text','position',[300 25 80 100]); handles.countertext1 = uicontrol('style','text','position',[40 20 80 20],'string','loop counter'); handles.countertext2 = uicontrol('style','text','position',[130 20 80 20],'string',''); %// initialize counter # of values calculated handles.calculatecounter = 0; handles.matrixvalues = zeros(1,2); guidata(handles.fig,handles); %// save handles structure of gui. important function calculatecallback(~,~) %// retrieve elements handles structure. handles = guidata(handles.fig); %// entries in text boxes. assumes inputs numbers. val1 = str2double(get(handles.val1edit,'string')); val2 = str2double(get(handles.val2edit,'string')); %// if 1st calculation, initialize matrix containing %// values. otherwise, concatenate current values existing matrix. if handles.calculatecounter == 0 handles.matrixvalues = [val1,val2]; else handles.matrixvalues = [handles.matrixvalues; val1,val2]; end %// print matrix values in text box. set(handles.matrixedit,'string',num2str(handles.matrixvalues)); %// update counter , display current value in gui. handles.calculatecounter = handles.calculatecounter + 1; set(handles.countertext2,'string',num2str(handles.calculatecounter)) guidata(handles.fig,handles); %// save handles structure of gui. end end
here screenshot of gui after calculations:
once you're done can fetch data handles.matrixvalues
, whatever want them. hope helps started!
Comments
Post a Comment