Filter löschen
Filter löschen

Matlab GUI: how to print slider value to workspace

3 Ansichten (letzte 30 Tage)
Tyler Martin
Tyler Martin am 27 Apr. 2017
Beantwortet: Jan am 28 Apr. 2017
Hello I am having some issue with my GUI. I am trying to set up a slider to create a time vector between two point. I have two sliders and will use one value and subtract the other value to create a time vector. Here is my GUI:
function varargout = Drive_Cycle_GUI(varargin)
% DRIVE_CYCLE_GUI MATLAB code for Drive_Cycle_GUI.fig
% DRIVE_CYCLE_GUI, by itself, creates a new DRIVE_CYCLE_GUI or raises the existing
% singleton*.
%
% H = DRIVE_CYCLE_GUI returns the handle to a new DRIVE_CYCLE_GUI or the handle to
% the existing singleton*.
%
% DRIVE_CYCLE_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DRIVE_CYCLE_GUI.M with the given input arguments.
%
% DRIVE_CYCLE_GUI('Property','Value',...) creates a new DRIVE_CYCLE_GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Drive_Cycle_GUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Drive_Cycle_GUI_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 Drive_Cycle_GUI
% Last Modified by GUIDE v2.5 26-Apr-2017 09:25:59
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Drive_Cycle_GUI_OpeningFcn, ...
'gui_OutputFcn', @Drive_Cycle_GUI_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 Drive_Cycle_GUI is made visible.
function Drive_Cycle_GUI_OpeningFcn(hObject, ~, handles, varargin)
% This function has no output args, see OutputFcn.
if true
% code
end
% 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 Drive_Cycle_GUI (see VARARGIN)
% Choose default command line output for Drive_Cycle_GUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Drive_Cycle_GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Drive_Cycle_GUI_OutputFcn(~, ~, 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} = get(handles.output);
% --- Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- 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)
axes(handles.axes2);
S.HWFET = xlsread('DriveCycles_Scaled.xlsx',5,'A4:B769');
S.LN = plot(S.HWFET(:,1),S.HWFET(:,2),'r');
S.leftmarker = line([0 0],get(handles.axes2,'Ylim'));
S.rightmarker = line([0 0],get(handles.axes2,'Ylim'));
S.sl_left = uicontrol('style','slide',...
'unit','pix',...
'position',[575 380 260 30],...
'min',1,'max',length(S.HWFET),'val',1,...
'sliderstep',[1/765 1/765],...
'callback',{@sl_left_call,S});
S.sl_right = uicontrol('style','slide',...
'unit','pix',...
'position',[575 320 260 30],...
'min',1,'max',length(S.HWFET),'val',1,...
'sliderstep',[1/765 1/765],...
'callback',{@sl_right_call,S});
axes(handles.axes3)
car = importdata('Car_Graphic.jpg');
image(car);
axis off
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes2);
S.FTP = xlsread('DriveCycles_Scaled.xlsx',4,'A4:B2481');
S.LN = plot(S.FTP(:,1),S.FTP(:,2),'r');
S.leftmarker = line([0 0],get(handles.axes2,'Ylim'));
S.rightmarker = line([0 0],get(handles.axes2,'Ylim'));
S.sl_left = uicontrol('style','slide',...
'unit','pix',...
'position',[575 380 260 30],...
'min',1,'max',length(S.FTP),'val',1,...
'sliderstep',[1/2477 1/2477],...
'callback',{@sl_left_call,S});
S.sl_right = uicontrol('style','slide',...
'unit','pix',...
'position',[575 320 260 30],...
'min',1,'max',length(S.FTP),'val',1,...
'sliderstep',[1/2477 1/2477],...
'callback',{@sl_right_call,S});
axes(handles.axes3)
car = importdata('Car_Graphic.jpg');
image(car);
axis off
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes2);
S.US06 = xlsread('DriveCycles_Scaled.xlsx',6,'A4:B600');
S.LN = plot(S.US06(:,1),S.US06(:,2),'r');
S.leftmarker = line([0 0],get(handles.axes2,'Ylim'));
S.rightmarker = line([0 0],get(handles.axes2,'Ylim'));
S.sl_left = uicontrol('style','slide',...
'unit','pix',...
'position',[575 380 260 30],...
'Tag', 'LeftSlider',...
'min',1,'max',length(S.US06),'val',1,...
'sliderstep',[1/596 1/596],...
'callback',{@sl_left_call,S});
S.sl_right = uicontrol('style','slide',...
'unit','pix',...
'position',[575 320 260 30],...
'min',1,'max',length(S.US06),'val',1,...
'sliderstep',[1/596 1/596],...
'callback',{@sl_right_call,S});
axes(handles.axes3)
car = importdata('Car_Graphic.jpg');
image(car);
axis off
function sl_left_call(varargin)
% Callback for the slider.
[h,S] = varargin{[1,3]}; % calling handle and data structure.
set(S.leftmarker,'Xdata',[get(h,'value') get(h,'value')])
% setappdata(S.leftmarker,'leftposition','value')
% getappdata(S.leftmarker,'leftposition')
function [] = sl_right_call(varargin)
% Callback for the slider.
[h,S] = varargin{[1,3]}; % calling handle and data structure.
set(S.rightmarker,'Xdata',[get(h,'value') get(h,'value')])
Right now everything works except not printing, I am in need of help on this part. I am pretty new to Matlab GUI.
Thank you
  7 Kommentare
Tyler Martin
Tyler Martin am 27 Apr. 2017
Can you explain that?
Adam
Adam am 28 Apr. 2017
You can put any code in e.g. pushbutton3_Callback that you could put on the command line so that is the usual way to respond to changes on the UI and have relevant code be automatically called rather than just dump parameters into the base workspace and wait for the functionality to be called afterwards from there. It seems to defeat the purpose of having a UI.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 28 Apr. 2017
Using the base workspace to collect data is a bad idea. It suffers from the same problems as global variables (ask a search engine for details).
The standard way to solve this is to collect values inside a GUI (search for "share data between callbacks" in this forum) and start the calculations e.g. by a button. Then you can run multiple instances of the GUI or different GUIs with interferences.
You create the sliders dynamically inside the callback. This will be very confusing, if the button is pressed mutiple times.

Weitere Antworten (0)

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by