Why did I this error Error in Matlab_Partical_fig1 (line 43).

3 views (last 30 days)
I don't understand why my brighten push button and darken buttion don't work?
function varargout = Matlab_Partical_fig1(varargin)
B = 1
% MATLAB_PARTICAL_FIG1 MATLAB code for Matlab_Partical_fig1.fig
% MATLAB_PARTICAL_FIG1, by itself, creates a new MATLAB_PARTICAL_FIG1 or raises the existing
% singleton*.
%
% H = MATLAB_PARTICAL_FIG1 returns the handle to a new MATLAB_PARTICAL_FIG1 or the handle to
% the existing singleton*.
%
% MATLAB_PARTICAL_FIG1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MATLAB_PARTICAL_FIG1.M with the given input arguments.
%
% MATLAB_PARTICAL_FIG1('Property','Value',...) creates a new MATLAB_PARTICAL_FIG1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Matlab_Partical_fig1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Matlab_Partical_fig1_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 Matlab_Partical_fig1
% Last Modified by GUIDE v2.5 14-Mar-2024 13:23:49
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Matlab_Partical_fig1_OpeningFcn, ...
'gui_OutputFcn', @Matlab_Partical_fig1_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 Matlab_Partical_fig1 is made visible.
function Matlab_Partical_fig1_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 Matlab_Partical_fig1 (see VARARGIN)
% Choose default command line output for Matlab_Partical_fig1
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Matlab_Partical_fig1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Matlab_Partical_fig1_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)
handles.filename = uigetfile('*.jpg');
handles.theOriginalImage = imread(handles.filename);
handles.theNewImage = handles.theOriginalImage;
image(handles.theOriginalImage);
guidata(hObject, 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)
% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject handle to checkbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox1
isChecked = get(hObject,'Value');
if(isChecked)
image(handles.theOriginalImage);
else
image(handles.theNewImage);
end
% --- Executes on button press in pushbutton2 Darken Button
function pushbutton2_Callback(hObject, eventdata, handles)
Ft = B ;
Sv = (Ft-.25)/Ft;
Nt = Sv * Ft ;
B = Nt
handles.theNewImage = handles.theNewImage .* (B)
image(handles.theNewImage);
guidata(hObject,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)
% --- Executes on button press in pushbutton3 Lighten Button
function pushbutton3_Callback(hObject, eventdata, handles)
Ft = B ;
Av = (Ft+.25)/Ft;
Nt = Av * Ft;
B = Nt
handles.theNewImage = handles.theNewImage .* (B)
image(handles.theNewImage);
guidata(hObject,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)
% --- Executes on button press in pushbutton5 Removes Color
function pushbutton5_Callback(hObject, eventdata, handles)
[row col pan] = size(handles.theNewImage);
for J = 1:row
for K = 1:col
averagedVal = (handles.theNewImage(J,K,1) +handles.theNewImage(J,K,2) +handles.theNewImage(J,K,3))/3;
handles.theNewImage(J,K,1) = averagedVal;
handles.theNewImage(J,K,2) = averagedVal;
handles.theNewImage(J,K,3) = averagedVal;
end
end
image(handles.theNewImage);
guidata(hObject,handles);
% hObject handle to pushbutton5 (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 pushbutton6 Save As
function pushbutton6_Callback(hObject, eventdata, handles)
[fileName, pathName] = uiputfile({'*.jpg', 'JPEG Image (*.jpg)'; '*.png', 'PNG Image (*.png)'}, 'Save As');
filePath = fullfile(pathName, fileName);
imwrite(handles.theNewImage, filePath);
% hObject handle to pushbutton4 (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 pushbutton8. Color Swap
function pushbutton8_Callback(hObject, eventdata, handles)
handles.theNewImage(:,:,1) = handles.theNewImage(:,:,2);
handles.theNewImage(:,:,2) = handles.theNewImage(:,:,3);
handles.theNewImage(:,:,3) = handles.theNewImage(:,:,1);
image(handles.theNewImage);
guidata(hObject,handles);
% hObject handle to pushbutton8 (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 pushbutton11. NO Red
function pushbutton11_Callback(hObject, eventdata, handles)
handles.theNewImage(:,:,1) = 0;
image(handles.theNewImage);
guidata(hObject,handles);
% hObject handle to pushbutton11 (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 pushbutton12 reset
function pushbutton12_Callback(hObject, eventdata, handles)
handles.theNewImage = handles.theOriginalImage;
image(handles.theNewImage);
guidata(hObject,handles);
% hObject handle to pushbutton12 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

Answers (1)

Voss
Voss on 18 Mar 2024
function pushbutton2_Callback(hObject, eventdata, handles)
Ft = B ;
B is not defined in that function.
B is defined in the main function:
function varargout = Matlab_Partical_fig1(varargin)
B = 1
but the callbacks are all local functions (a.k.a. subfunctions of the main function) with their own workspaces, so you'd need to define B in each function where you want to use it, or better, put it in the handles structure and use that.
  1 Comment
Voss
Voss on 18 Mar 2024
How to put B in the handles structure and use it:
1. Modify the Opening Function to store B in the handles structure:
% --- Executes just before Matlab_Partical_fig1 is made visible.
function Matlab_Partical_fig1_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 Matlab_Partical_fig1 (see VARARGIN)
% Choose default command line output for Matlab_Partical_fig1
handles.output = hObject;
handles.B = 1;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Matlab_Partical_fig1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
2. Anywhere you want to use that B, refer to handles.B:
% --- Executes on button press in pushbutton2 Darken Button
function pushbutton2_Callback(hObject, eventdata, handles)
Ft = handles.B ;
Sv = (Ft-.25)/Ft;
Nt = Sv * Ft ;
B = Nt
handles.theNewImage = handles.theNewImage .* (B)
image(handles.theNewImage);
guidata(hObject,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)

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!