MATLAB中GUI设计

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MATLAB中GUI设计相关的知识,希望对你有一定的参考价值。

MATLAB的GUI中,我想将两个Edit Text和一个Button连起来,在其中一个Edit Text中输入内容,然後单击Button,在另一个Edit Text中输出相应的内容该怎麼编写呀?

参考技术A %初始化一个图
FigName = 'Gui Test';
FigH = findobj(0,'Name',FigName);
if ~isempty(FigH)
close(FigH);
end
scrsz = get(0,'ScreenSize');
FigH = figure( ...
'Name', FigName, ...
'NumberTitle', 'off',...
'Position',[scrsz(1)+0.05*scrsz(3) scrsz(2)+0.05*scrsz(4) 0.3*scrsz(3) 0.3*scrsz(4)]);

figPos = get(FigH, 'position');
axis_area=1;
axisPos = [figPos(1) figPos(2) figPos(3) figPos(4)*axis_area];
axisPos = axisPos./[figPos(1) figPos(2) figPos(3) figPos(4)];
AxisH = ...
axes('unit', 'normal', 'pos', [.1 .1 .8 .8], 'visible', 'off','XDir','reverse'); max_x = 12; max_y = 6;
set(AxisH, 'clim', [1 64], ...
'xlim', [0 max_x], ...
'ylim', [0 max_y], ...
'box', 'off');
axis equal;
%设置颜色、单位
framecolor = 222/255*[1 1 1];
controlcolor = 232/255*[1 1 1];
set(AnimThwcFigH, 'defaultuicontrolunits' , 'normalized' );%放置frame
uicontrol( 'style' , 'frame' , ... % setting ui area
'position' ,[0.1,0.5,0.8,0.2],...
'BackgroundColor',framecolor);
%edit box A
textAH=uicontrol(FigH, 'style' , 'edit' , ...
'position' ,[0.11,.51,0.2,0.12], ...
'visible','on',...
'BackgroundColor',controlcolor,...
'FontSize',13);
%edit box B
textBH=uicontrol(FigH, 'style' , 'edit' , ...
'position' ,[0.69,.51,0.2,0.12], ...
'visible','on',...
'BackgroundColor',controlcolor,...
'FontSize',13);
%push button
ButtonH = uicontrol(FigH, 'style' , 'push' , ...
'string' , ' Copy A to B ' , ...
'position' ,[0.4,.51,0.2,0.12],...
'fontsize',10,...
'BackgroundColor',controlcolor,...
'callback','set(textBH,''string'',get(textAH,''String''))');
%call back 函数复制A的内容到B

游戏基于matlab GUI音乐时钟设计含Matlab源码 1104期

一、简介

基于matlab GUI音乐时钟设计

二、源代码

function varargout = untitled(varargin)
% UNTITLED M-file for untitled.fig
%      UNTITLED, by itself, creates a new UNTITLED or raises the existing
%      singleton*.
%
%      H = UNTITLED returns the handle to a new UNTITLED or the handle to
%      the existing singleton*.
%
%      UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in UNTITLED.M with the given input arguments.
%
%      UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
%      existing singleton*.  Starting from the left, property value pairs ar
%      applied to the GUI before untitled_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to untitled_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help untitled

% Last Modified by GUIDE v2.5 02-Jun-2021 00:03:55

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @untitled_OpeningFcn, ...
                   'gui_OutputFcn',  @untitled_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to untitled (see VARARGIN)
if ~isempty(timerfindall), stop(timerfindall);  delete(timerfindall);end
set(handles.radiobutton2,'value',handles.clockSaveData(3));
temp=get(handles.uipanel3,'Units');
set(handles.uipanel3,'Units','pixels');
position=get(handles.uipanel3,'position');
position(4)=position(2)-17;
position(2)=12;
position(3)=position(3)+position(1)-49;
handles.sound_player= actxcontrol('wmplayer.ocx.7',position ,handles.figure1);
handles.sound_player.settings.volume=100;
set(handles.uipanel3,'Units',temp);


ha=axes('units','normalized','position',[0 0 1 1]); 
uistack(ha,'down') 
II=imread('backgroud_picture.jpg'); 
image(II) 
colormap gray 	
set(ha,'handlevisibility','off','visible','off'); 


handles.timer=timer;
set(handles.timer,'TimerFcn',{@timer_action,handles});
start_timer(handles);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%start_timer_solid_hour(handles);**************************************************
if (get(handles.checkbox1,'Value')==1)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    start_timer_solid_hour(handles);
else
    start_timer(handles);
end
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);


filename='alarm_icon.jpg';
javaFrame=get(hObject,'javaFrame');
set(javaFrame,'FigureIcon',javax.swing.ImageIcon(filename));

% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = untitled_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- 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)
[filename,pathname]=uigetfile(...
    {'*.mp3;*.wav;*.mkv','可播放';},'选择定时播放','MultiSelect','off');
handles.musicSaveData=fullfile(pathname,filename);
 
set(handles.edit_musicFile,'String',handles.musicSaveData);
musicSaveData=handles.musicSaveData;
save('musicSaveData.txt','-ascii','musicSaveData');
handles.pushbutton1=hObject;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
guidata(hObject,handles);



% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1
handles.clockSaveData(1)=get(hObject,'Value');
clockSaveData=handles.clockSaveData;
save('clockSaveData.txt','-ascii','clockSaveData');
guidata(hObject,handles);
start_timer(handles);

% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
set(hObject,'Value',handles.clockSaveData(1));
handles.popupmenu1=hObject;
guidata(hObject,handles);


% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu2
handles.clockSaveData(2)=get(hObject,'Value');
clockSaveData=handles.clockSaveData;
save('clockSaveData.txt','-ascii','clockSaveData');
guidata(hObject,handles);
start_timer(handles);



% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
set(hObject,'Value',handles.clockSaveData(2));
handles.popupmenu2=hObject;
guidata(hObject,handles);


function edit_musicFile_Callback(hObject, eventdata, handles)
% hObject    handle to edit_musicFile (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 edit_musicFile as text
%        str2double(get(hObject,'String')) returns contents of edit_musicFile as a double
handles.musicSaveData=get(hObject,'String');
musicSaveData=handles.musicSaveData;
save('musicSaveData.txt','-ascii','musicSaveData');
guidata(hObject,handles);


% --- Executes during object creation, after setting all properties.
function edit_musicFile_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_musicFile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
set(hObject,'String',handles.musicSaveData); 
handles.edit_musicFile=hObject;
guidata(hObject,handles);


% --- Executes during object creation, after setting all properties.
function pushbutton1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
handles.pushbutton1=hObject;
guidata(hObject,handles);

三、运行结果

在这里插入图片描述

四、备注

版本:2014a

以上是关于MATLAB中GUI设计的主要内容,如果未能解决你的问题,请参考以下文章

matlab gui界面设计日历用啥编辑器

MATLAB中GUI设计

MATLAB 使用GUI设计简单的计算器

MATLAB GUI

matlab gui 设计 日志

matlab gui 设计 日志