2016-01-08 38 views
0

我開發了一個GUI來跟蹤投注基金。它會告訴您相對於您的基金規模下注多少。它的基礎是它有2個按鈕,無論是贏或輸。每次被按下時,它都會更新基金規模,並告訴您下注(10%)哪裏的贏/輸率是雙倍或什麼都沒有。Matlab GUI每次按下按鈕更新圖形

一個例子如下圖所示。如果初始資金爲£1000,我們讓勝利是= 1,和損失= 0

enter image description here

當按鈕被按下,只顯示了新基金的規模和賭注大小。我正在尋找一種方法來繪製結果,以便它將所有newFundSize(y軸)與count(x軸)進行比較,以便顯示您的基金規模隨着時間的變化(投注數量)如何變化。

它目前將x軸更改爲正確的長度, x1 = linspace(1:currentCount)。 對於Y值,我只能記得以前的基金規模和新的基金規模。

是否有可能以某種方式繪製所有新的基金大小數據?或者儲存所有以前的基金規模值?

另外它有一種方法來存儲訂單和輸贏次數按鈕的次數?

例如,贏,贏,輸,贏,輸,贏將被存儲爲[1 1 0 1 0 1]?

謝謝。

+0

你能重新格式化第二段嗎?我不明白這個例子。謝謝! –

+0

是的,我完成了。希望能讓它更容易理解。 – user3420834

回答

1

是的,可以在MATLAB中存儲所有這些信息。有幾種方法可以做到這一點,但最好的方法通常是將數據存儲在數字本身中。您可以使用圖中的guidata,setappdata and getappdataUserData屬性來執行此操作。

下面我有使用setappdata方法,其中餘存儲在一個數據結構中的信息,這些信息結構的陣列,它具有以下形式

data = 

     win: true % Logical indicating whether it was a winning bet 
    fund: 1000 % Amount of money in the fund 
    count: 0  % The number of bets placed so far (optional) 

每次用戶將賭注,我追加一個例子數據結構的上述格式的另一種結構。

這裏是完整的例子。

function data = bet(initialFund, betPercentage) 
    if ~exist('initialBet', 'var') 
     initialFund = 1000; 
    end 

    if ~exist('betPercentage', 'var') 
     betPercentage = 10; 
    end 

    % The data structure that we will use to keep track of the bets 
    data = struct('win', NaN, 'fund', initialFund, 'count', 0); 

    % Now create the figure and the plot that you want 
    fig = figure(); 

    hax = axes(... 
     'Parent', fig, ... 
     'Units', 'normalized', ... 
     'Position', [0.15 0.35 0.7 0.6]); 

    % Plot to display current fund data 
    plt = plot(NaN, NaN, 'Parent', hax); 

    xlabel(hax, 'Bet', 'FontWeight', 'bold', 'FontSize', 18) 
    ylabel(hax, 'Fund ($)', 'FontWeight', 'bold', 'FontSize', 18) 

    set(hax, 'FontWeight', 'bold') 

    % Create a button to place a winning and losing bet 
    uicontrol(fig, ... 
     'String', 'Place Winning Bet', ... 
     'Units', 'normalized', ... 
     'Position', [0.01 0.01 0.45 0.2], ... 
     'Callback', @(s,e)place_bet(true, betPercentage/100)); 

    uicontrol(fig, ... 
     'String', 'Place Losing Bet', ... 
     'Units', 'normalized', ... 
     'Position', [0.5 0.01 0.45 0.2], ... 
     'Callback', @(s,e)place_bet(false, betPercentage/100)); 

    % Store the data within the handle 
    setappdata(fig, 'BetData', data) 

    % Plot the initial bet data 
    refreshPlot(data); 

    function place_bet(win_lose, percentage) 
     % Determine whether we win or lose 
     data = getappdata(fig, 'BetData'); 

     % Now add the new data 
     lastbet = data(end); 

     % Compute the new fund based on the bet amount 
     newfund = (1 - ((-1)^win_lose) * percentage) * lastbet.fund; 

     newdata = struct(... 
      'win', win_lose, ... 
      'fund', newfund, ... 
      'count', lastbet.count + 1); 

     data = cat(1, data, newdata); 

     % Store the updated data 
     setappdata(fig, 'BetData', data) 

     % Now update the plot 
     refreshPlot(data); 
    end 

    function refreshPlot(data) 
     set(plt, 'XData', [data.count], 'YData', [data.fund]) 
     set(hax, 'XLim', [data(1).count, max(data(end).count, 10)]) 
    end 
end