matlab gui實現滑動條和靜態文本同步顯示

這個小程序主要實現了輸入n次交易和p個物品,隨機產生n行p列的購物清單。其中p值最大不能超過30列,因爲我設計的編碼數組最大隻有30列。

1、產生購物清單的程序代碼:

function T=produce_T(n,p)
% 產生隨機交易矩陣
T= randi([0,1],n,p);
% 編碼庫  大概有30類物品
MyCodes={'奶酪','糖果','足球','衣服','靴子','雞肉','洗面奶','沐浴露','洗髮水','梳子','鏡子','電池','髮夾','戒指','胸針','複印機','耳機','字典','充電器','圍巾','領帶','西服','飲水機','檯球','指南針','毛巾','熱水瓶','水杯','鼠標','衛生紙'};
Total_num=length(MyCodes);
% 產生編碼規則矩陣 randperm(N,K) 產生1-N的不重複的數,K個
code=MyCodes(randperm(Total_num,p));

% 生成購物清單txt
fid=fopen('mygoods.txt','w');
for i=1:size(T,1)
    % 獲得元素爲1的列下標
    [~,ib]=find(T(i,:)==1);
    % 進行編碼 將cell數組轉化爲字符數組
    code1=code(ib);
    % 因爲不同的cell之間不會有空格,需要手動添加
    code2=cellfun(@(u)[u,' '],code1(1:end-1),'UniformOutput',false);
    str_arry=cell2mat(code2);
    % 寫入文件
    fprintf(fid,'%s\n',str_arry);
end
fclose(fid);
disp(['生成購物清單到''mygoods.txt' '完成!']);

購物清單如圖所示:n=10,p=20。


2、設計matlab的GUI界面

2.1、fig界面: 


需要注意靜態文本框的max需要大於1,進度條默認爲0-1之間變化。

2.2 相關代碼

2.2.1讀取編輯框中的交易次數:

function n_Callback(hObject, eventdata, handles)
% hObject    handle to n (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 n as text
%        str2double(get(hObject,'String')) returns contents of n as a double
n=str2double(get(hObject,'String'))
if n<=1
    errordlg('交易次數不能少於1')
elseif n~=fix(n) 
    errordlg('輸入必須爲整數');
end

2.2.2讀取編輯框中的物品種類:

function p_Callback(hObject, eventdata, handles)
% hObject    handle to p (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 p as text
%        str2double(get(hObject,'String')) returns contents of p as a double
p=str2double(get(hObject,'String'))
if p<=1 || p>30
    errordlg('物品個數不能少於1或者多於30');
elseif p~=fix(p)
    errordlg('輸入必須爲整數');
end

我做了一些輸入校驗,不能輸入除整數之外的字符。

2.2.3 點擊確認按鈕之後的事件:

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
n=str2double(get(handles.n,'String'))
p=str2double(get(handles.p,'String'))
if n<=1 || p<=1 || p>30
    errordlg('交易次數或物品個數不能小於1');
    return
elseif n~=fix(n) || p~=fix(p)
    errordlg('輸入必須爲整數');
    return
else
    T=produce_T(n,p);
    % 加載文檔中的內容
    ex=importdata('mygoods.txt');
    row=size(ex,1)
    if row>24 % 24假設爲最大顯示行數
        set(handles.T_data,'String',ex(1:24));
        % 設置滑動條位置 不能取整
        x=24/row;
        set(handles.slider1,'Value',(1-x));
    else
        set(handles.T_data,'String',ex);
        % 設置滑動條位置
        set(handles.slider1,'Value',0);
    end
  
end
produce_T(n,p)是最前面寫的購物清單的代碼。

我設置的最大顯示行數爲24行,小於24,就直接將滑動條位置移動到最底部,不能在下滑。大於24,則計算24行所佔總行數的比例,設置滑動條處於計算出來的位置。

效果圖如下:


2.2.4 滑動條移動的事件

% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

ex=importdata('mygoods.txt');
row=size(ex,1)
if row>24
%     set(hObject,'Value',1-24/row);
    slider = get(hObject,'Value')
    x1=1-slider;
    % 取得當前需要顯示的數據量
    count=round(row*x1)
    if count>24
        % 顯示對應數據
        set(handles.T_data,'String',ex(count-24:count));
    end
else
     % 顯示對應數據
     set(handles.T_data,'String',ex(1:row));
     set(hObject,'Value',0);
end

如果總的行數小於24的話,則直接顯示所有數據,並將滑動條置於底部。若大於24的話,則先獲取當前的滑動條值,再計算出當前所需要顯示的數據量,最後顯示當前數據量-24——當前數據量區間的數據。

效果圖:


txt文件:




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章