From: carlos on
Hello everyone,

I try to explain properly hehe. I need to add a function, created by myself, within a guide file (.m).
I've got one button and three edit texts where in two of them I insert two numbers. After a click on the button, the third edit text must show the outcome of my function.
The problem appears when I do click, the outcome in the last edit text is NaN. I don't know how i can do it cause I've used the command num2str but it doesn't work :S
Could you help me??

My function is more complicated but i give you an example:

function a=sumar(n1,n2)
a=n1+n2;
end


CHEERS!!!
From: Image Analyst on
carlos:
Assuming you've read the two edit fields and converted them successfully into two numbers n1 and n2, then your function should work. Back in the calling function you'll need to do something like

% In the push button callback:
num1=str2double(get(handles.edit1, 'string'));
num2=str2double(get(handles.edit2, 'string'));
a1 = sumar(num1, num2);
set(handles.edit3, 'string', num2str(a1));

That should work. If it doesn't, you might have to post your actual code. Maybe you didn't convert to numbers properly and you're trying to add two strings together or something illegal.
From: carlos on
"Image Analyst" <imageanalyst(a)mailinator.com> wrote in message <hr5cbv$od5$1(a)fred.mathworks.com>...
> carlos:
> Assuming you've read the two edit fields and converted them successfully into two numbers n1 and n2, then your function should work. Back in the calling function you'll need to do something like
>
> % In the push button callback:
> num1=str2double(get(handles.edit1, 'string'));
> num2=str2double(get(handles.edit2, 'string'));
> a1 = sumar(num1, num2);
> set(handles.edit3, 'string', num2str(a1));
>
> That should work. If it doesn't, you might have to post your actual code. Maybe you didn't convert to numbers properly and you're trying to add two strings together or something illegal.

Before all, thank you for helping me!!!

I've tried to do your recommendations but it doesn't work yet. Perhaps my conversion to layman's terms is my fault. Below, I attach you my guide "script" and afterwards my own function "script". The button is called "pushbutton1_Callback" and the edit text where the outcome must appear is called "dato_resultado".

-------------------------------------------------------------------------------------------------------------------
function varargout = valorador(varargin)
% VALORADOR M-file for valorador.fig
% VALORADOR, by itself, creates a new VALORADOR or raises the existing
% singleton*.
%
% H = VALORADOR returns the handle to a new VALORADOR or the handle to
% the existing singleton*.
%
% VALORADOR('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in VALORADOR.M with the given input arguments.
%
% VALORADOR('Property','Value',...) creates a new VALORADOR or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before valorador_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to valorador_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 valorador

% Last Modified by GUIDE v2.5 26-Apr-2010 00:12:39

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @valorador_OpeningFcn, ...
'gui_OutputFcn', @valorador_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 valorador is made visible.
function valorador_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for valorador
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);


% --- Outputs from this function are returned to the command line.
function varargout = valorador_OutputFcn(hObject, eventdata, handles)

varargout{1} = handles.output;


% --- Executes on selection change in opciones_valoradores.
function opciones_valoradores_Callback(hObject, eventdata, handles)
opcion=get(handles.opciones_valoradores,'Value');
handles.opciones_valoradores=opcion;
guidata(hObject,handles); %Salvamos los datos.



% --- Executes during object creation, after setting all properties.
function opciones_valoradores_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end



function edit1_Callback(hObject, eventdata, handles)
k=get(hObject,'String'); %Tomamos el valor del strike.
k=str2double(k); %Convertimos el valor a número.
handles.edit1=k; %Hacemos que salga el valor del strike para todas las funciones.
guidata(hObject,handles); %Salvamos los datos.


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end



function edit2_Callback(hObject, eventdata, handles)
s0=get(hObject,'String'); %Tomamos el valor del producto.
s0=str2double(s0); %Convertimos el valor a número.
handles.edit2=s0; %Hacemos que salga el valor del producto para todas las funciones.
guidata(hObject,handles); %Salvamos los datos.



% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end



function edit3_Callback(hObject, eventdata, handles)
T=get(hObject,'String'); %Tomamos el valor del strike.
T=str2double(T); %Convertimos el valor a número.
handles.edit3=T; %Hacemos que salga el valor del strike para todas las funciones.
guidata(hObject,handles); %Salvamos los datos.

% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end



function edit4_Callback(hObject, eventdata, handles)
sigma=get(hObject,'String'); %Tomamos el valor del strike.
sigma=str2double(sigma); %Convertimos el valor a número.
handles.edit4=sigma; %Hacemos que salga el valor del strike para todas las funciones.
guidata(hObject,handles); %Salvamos los datos.



% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end


% --- Executes when entered data in editable cell(s) in uitable2.
function uitable2_CellEditCallback(hObject, eventdata, handles)



% --- Executes when selected cell(s) is changed in uitable2.
function uitable2_CellSelectionCallback(hObject, eventdata, handles)



% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over opciones_valoradores.
function opciones_valoradores_ButtonDownFcn(hObject, eventdata, handles)



% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% Inputs necesarios
k=get(handles.edit1,'String');
k=str2double(k);
s0=get(handles.edit2,'String');
s0=str2double(s0);
T=get(handles.edit3,'String');
T=str2double(T);
sigma=get(handles.edit4,'String');
sigma=str2double(sigma);
N=get(handles.edit6,'String');
N=str2double(N);
dt=str2double(T/N);
d=str2double(exp(-sigma*sqrt(dt)));
u=str2double(exp(sigma*sqrt(dt)));
intereses=get(handles.edit7,'String');
int0=log(1+intereses);
int0=str2double(int0);
disp(int0)

% Seleccionamos el producto que tenemos y calculamos su función
opcion=handles.opciones_valoradores;
switch opcion
case 1
mensaje='Elige otra opción';
set(handles.dato_resultado,'String',mensaje);
case 2
b=put_europea(s0,k,dt,d,u,N,int0);
set(handles.dato_resultado,'string',num2str(b));
case 3
a=put_europea(s0,k,dt,d,u,N,int0);
a=num2str(a);
set(handles.dato_resultado,'String',a);
case 4
a=put_europea(s0,k,dt,d,u,N,int0);
a=num2str(a);
set(handles.dato_resultado,'String',a);
case 5
a=put_europea(s0,k,dt,d,u,N,int0);
a=num2str(a);
set(handles.dato_resultado,'String',a);
otherwise
mensaje='Elige otra opción';
set(handles.dato_resultado,'String',mensaje);
end
guidata(hObject,handles); %Salvamos los datos.


function dato_resultado_Callback(hObject, eventdata, handles)


% --- Executes during object creation, after setting all properties.
function dato_resultado_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end



function edit6_Callback(hObject, eventdata, handles)
N=get(hObject,'String'); %Tomamos el valor del strike.
N=str2double(N); %Convertimos el valor a número.
handles.edit7=N; %Hacemos que salga el valor del strike para todas las funciones.
guidata(hObject,handles); %Salvamos los datos.


% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end



function edit7_Callback(hObject, eventdata, handles)
int0=get(hObject,'String'); %Tomamos el valor del strike.
int0=str2double(int0); %Convertimos el valor a número.
handles.edit7=int0; %Hacemos que salga el valor del strike para todas las funciones.
guidata(hObject,handles); %Salvamos los datos.



% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end


% --- Executes on key press with focus on edit1 and none of its controls.
function edit1_KeyPressFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata structure with the following fields (see UICONTROL)
% Key: name of the key that was pressed, in lower case
% Character: character interpretation of the key(s) that was pressed
% Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles structure with handles and user data (see GUIDATA)


----------------------------------------------------------------------------------------------------------------


function [b]=put_europea(s0,k,dt,d,u,N,int0)
%% Cálculo de la medida neutral al riesgo
p=(exp(int0*dt)-d)/(u-d);

%% Generación del último paso del árbol
subyacente=zeros(N+1,1);
for j=1:N+1
subyacente(j,1)=s0*(u^((N+1)-j))*(d^(j-1));
disp(subyacente)
end

%% Árbol de precios
precio=zeros(N+1,N+1);
precio(:,N+1)=max(k-subyacente,0);
for j=N:-1:1
for i=1:j
precio(i,j)=exp(-int0*dt)*(p*precio(i,j+1)+(1-p)*precio(i+1,j+1));
end
end
b=precio(1,1);
end



Many thanks,

Carlos.
From: carlos on
"carlos " <parryonline(a)wanadoo.es> wrote in message <hr7j6q$hv8$1(a)fred.mathworks.com>...
> "Image Analyst" <imageanalyst(a)mailinator.com> wrote in message <hr5cbv$od5$1(a)fred.mathworks.com>...
> > carlos:
> > Assuming you've read the two edit fields and converted them successfully into two numbers n1 and n2, then your function should work. Back in the calling function you'll need to do something like
> >
> > % In the push button callback:
> > num1=str2double(get(handles.edit1, 'string'));
> > num2=str2double(get(handles.edit2, 'string'));
> > a1 = sumar(num1, num2);
> > set(handles.edit3, 'string', num2str(a1));
> >
> > That should work. If it doesn't, you might have to post your actual code. Maybe you didn't convert to numbers properly and you're trying to add two strings together or something illegal.
>
> Before all, thank you for helping me!!!
>
> I've tried to do your recommendations but it doesn't work yet. Perhaps my conversion to layman's terms is my fault. Below, I attach you my guide "script" and afterwards my own function "script". The button is called "pushbutton1_Callback" and the edit text where the outcome must appear is called "dato_resultado".
>
> -------------------------------------------------------------------------------------------------------------------
> function varargout = valorador(varargin)
> % VALORADOR M-file for valorador.fig
> % VALORADOR, by itself, creates a new VALORADOR or raises the existing
> % singleton*.
> %
> % H = VALORADOR returns the handle to a new VALORADOR or the handle to
> % the existing singleton*.
> %
> % VALORADOR('CALLBACK',hObject,eventData,handles,...) calls the local
> % function named CALLBACK in VALORADOR.M with the given input arguments.
> %
> % VALORADOR('Property','Value',...) creates a new VALORADOR or raises the
> % existing singleton*. Starting from the left, property value pairs are
> % applied to the GUI before valorador_OpeningFcn gets called. An
> % unrecognized property name or invalid value makes property application
> % stop. All inputs are passed to valorador_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 valorador
>
> % Last Modified by GUIDE v2.5 26-Apr-2010 00:12:39
>
> % Begin initialization code - DO NOT EDIT
> gui_Singleton = 1;
> gui_State = struct('gui_Name', mfilename, ...
> 'gui_Singleton', gui_Singleton, ...
> 'gui_OpeningFcn', @valorador_OpeningFcn, ...
> 'gui_OutputFcn', @valorador_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 valorador is made visible.
> function valorador_OpeningFcn(hObject, eventdata, handles, varargin)
> % Choose default command line output for valorador
> handles.output = hObject;
>
> % Update handles structure
> guidata(hObject, handles);
>
>
> % --- Outputs from this function are returned to the command line.
> function varargout = valorador_OutputFcn(hObject, eventdata, handles)
>
> varargout{1} = handles.output;
>
>
> % --- Executes on selection change in opciones_valoradores.
> function opciones_valoradores_Callback(hObject, eventdata, handles)
> opcion=get(handles.opciones_valoradores,'Value');
> handles.opciones_valoradores=opcion;
> guidata(hObject,handles); %Salvamos los datos.
>
>
>
> % --- Executes during object creation, after setting all properties.
> function opciones_valoradores_CreateFcn(hObject, eventdata, handles)
> if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
> set(hObject,'BackgroundColor','white');
> end
>
>
>
> function edit1_Callback(hObject, eventdata, handles)
> k=get(hObject,'String'); %Tomamos el valor del strike.
> k=str2double(k); %Convertimos el valor a número.
> handles.edit1=k; %Hacemos que salga el valor del strike para todas las funciones.
> guidata(hObject,handles); %Salvamos los datos.
>
>
> % --- Executes during object creation, after setting all properties.
> function edit1_CreateFcn(hObject, eventdata, handles)
> if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
> set(hObject,'BackgroundColor','white');
> end
>
>
>
> function edit2_Callback(hObject, eventdata, handles)
> s0=get(hObject,'String'); %Tomamos el valor del producto.
> s0=str2double(s0); %Convertimos el valor a número.
> handles.edit2=s0; %Hacemos que salga el valor del producto para todas las funciones.
> guidata(hObject,handles); %Salvamos los datos.
>
>
>
> % --- Executes during object creation, after setting all properties.
> function edit2_CreateFcn(hObject, eventdata, handles)
> if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
> set(hObject,'BackgroundColor','white');
> end
>
>
>
> function edit3_Callback(hObject, eventdata, handles)
> T=get(hObject,'String'); %Tomamos el valor del strike.
> T=str2double(T); %Convertimos el valor a número.
> handles.edit3=T; %Hacemos que salga el valor del strike para todas las funciones.
> guidata(hObject,handles); %Salvamos los datos.
>
> % --- Executes during object creation, after setting all properties.
> function edit3_CreateFcn(hObject, eventdata, handles)
> if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
> set(hObject,'BackgroundColor','white');
> end
>
>
>
> function edit4_Callback(hObject, eventdata, handles)
> sigma=get(hObject,'String'); %Tomamos el valor del strike.
> sigma=str2double(sigma); %Convertimos el valor a número.
> handles.edit4=sigma; %Hacemos que salga el valor del strike para todas las funciones.
> guidata(hObject,handles); %Salvamos los datos.
>
>
>
> % --- Executes during object creation, after setting all properties.
> function edit4_CreateFcn(hObject, eventdata, handles)
> if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
> set(hObject,'BackgroundColor','white');
> end
>
>
> % --- Executes when entered data in editable cell(s) in uitable2.
> function uitable2_CellEditCallback(hObject, eventdata, handles)
>
>
>
> % --- Executes when selected cell(s) is changed in uitable2.
> function uitable2_CellSelectionCallback(hObject, eventdata, handles)
>
>
>
> % --- If Enable == 'on', executes on mouse press in 5 pixel border.
> % --- Otherwise, executes on mouse press in 5 pixel border or over opciones_valoradores.
> function opciones_valoradores_ButtonDownFcn(hObject, eventdata, handles)
>
>
>
> % --- Executes on button press in pushbutton1.
> function pushbutton1_Callback(hObject, eventdata, handles)
> % Inputs necesarios
> k=get(handles.edit1,'String');
> k=str2double(k);
> s0=get(handles.edit2,'String');
> s0=str2double(s0);
> T=get(handles.edit3,'String');
> T=str2double(T);
> sigma=get(handles.edit4,'String');
> sigma=str2double(sigma);
> N=get(handles.edit6,'String');
> N=str2double(N);
> dt=str2double(T/N);
> d=str2double(exp(-sigma*sqrt(dt)));
> u=str2double(exp(sigma*sqrt(dt)));
> intereses=get(handles.edit7,'String');
> int0=log(1+intereses);
> int0=str2double(int0);
> disp(int0)
>
> % Seleccionamos el producto que tenemos y calculamos su función
> opcion=handles.opciones_valoradores;
> switch opcion
> case 1
> mensaje='Elige otra opción';
> set(handles.dato_resultado,'String',mensaje);
> case 2
> b=put_europea(s0,k,dt,d,u,N,int0);
> set(handles.dato_resultado,'string',num2str(b));
> case 3
> a=put_europea(s0,k,dt,d,u,N,int0);
> a=num2str(a);
> set(handles.dato_resultado,'String',a);
> case 4
> a=put_europea(s0,k,dt,d,u,N,int0);
> a=num2str(a);
> set(handles.dato_resultado,'String',a);
> case 5
> a=put_europea(s0,k,dt,d,u,N,int0);
> a=num2str(a);
> set(handles.dato_resultado,'String',a);
> otherwise
> mensaje='Elige otra opción';
> set(handles.dato_resultado,'String',mensaje);
> end
> guidata(hObject,handles); %Salvamos los datos.
>
>
> function dato_resultado_Callback(hObject, eventdata, handles)
>
>
> % --- Executes during object creation, after setting all properties.
> function dato_resultado_CreateFcn(hObject, eventdata, handles)
>
> if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
> set(hObject,'BackgroundColor','white');
> end
>
>
>
> function edit6_Callback(hObject, eventdata, handles)
> N=get(hObject,'String'); %Tomamos el valor del strike.
> N=str2double(N); %Convertimos el valor a número.
> handles.edit7=N; %Hacemos que salga el valor del strike para todas las funciones.
> guidata(hObject,handles); %Salvamos los datos.
>
>
> % --- Executes during object creation, after setting all properties.
> function edit6_CreateFcn(hObject, eventdata, handles)
>
> if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
> set(hObject,'BackgroundColor','white');
> end
>
>
>
> function edit7_Callback(hObject, eventdata, handles)
> int0=get(hObject,'String'); %Tomamos el valor del strike.
> int0=str2double(int0); %Convertimos el valor a número.
> handles.edit7=int0; %Hacemos que salga el valor del strike para todas las funciones.
> guidata(hObject,handles); %Salvamos los datos.
>
>
>
> % --- Executes during object creation, after setting all properties.
> function edit7_CreateFcn(hObject, eventdata, handles)
>
> if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
> set(hObject,'BackgroundColor','white');
> end
>
>
> % --- Executes on key press with focus on edit1 and none of its controls.
> function edit1_KeyPressFcn(hObject, eventdata, handles)
> % hObject handle to edit1 (see GCBO)
> % eventdata structure with the following fields (see UICONTROL)
> % Key: name of the key that was pressed, in lower case
> % Character: character interpretation of the key(s) that was pressed
> % Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
> % handles structure with handles and user data (see GUIDATA)
>
>
> ----------------------------------------------------------------------------------------------------------------
>
>
> function [b]=put_europea(s0,k,dt,d,u,N,int0)
> %% Cálculo de la medida neutral al riesgo
> p=(exp(int0*dt)-d)/(u-d);
>
> %% Generación del último paso del árbol
> subyacente=zeros(N+1,1);
> for j=1:N+1
> subyacente(j,1)=s0*(u^((N+1)-j))*(d^(j-1));
> disp(subyacente)
> end
>
> %% Árbol de precios
> precio=zeros(N+1,N+1);
> precio(:,N+1)=max(k-subyacente,0);
> for j=N:-1:1
> for i=1:j
> precio(i,j)=exp(-int0*dt)*(p*precio(i,j+1)+(1-p)*precio(i+1,j+1));
> end
> end
> b=precio(1,1);
> end
>
>
>
> Many thanks,
>
> Carlos.



I forgot something. These "scripts" are in different files. The former one is called valorador.m(and the co-named valorador.fig) and the latter one is called as the function put_europea.m.

Cheers!!
From: carlos on
Finally I've got it!!! I had several problems with the formats.

Thanks for your time, you are so great!