Ich möchte nun in einer Extra funktion diese Variablen mit den zugehörigen Werten in einem ASCII File Speichern.
Mein Problem ist jetzt in meiner weiteren Funktion an die Werte und Variablen der vorangegangenen Funktionen ranzukommen.
Hat Jemand eine Idee wie das am besten gelöst werden könnte?
Verfasst am: 19.09.2011, 10:31
Titel: Re: Variablen aus Funktion auslesen
Hallo morisq,
Du könntest die Variablen doch einfach als Inpzut für die folgende Funktion verwenden. Wenn Dir das wegen einer großen Zahl an Variablen zu uinübersichtlich wird, packe sie einfach in ein STRUCT.
initialisieren?
oder ausserhalb?
Ich muss evtl dazuschreiben das ich ein GUI programmiere und da nicht so recht weiß an welche stelle ich globale variablendeklarationen setzen soll.
Hallo Jan, das klingt gut, aber ich weiß nicht so recht wie ich dei Variablen als input für meine folgende Funktion verwenden kann. Hättest du ein kleines Beispiel in Codeform?
MfG
Ich glaube ich poste am besten mal mein Programm bisher. Es soll einen elementaren Taschenrechner darstellen der + - * / rechnet.
Ich möchte in einer extra Funktion die zwischenwerte wie a_sum,b_sum etc. in ein ASCII File speichern. Hier mein Code:
Code:
functionvarargout = BasicCalculator(varargin) % BASICCALCULATOR M-file for BasicCalculator.fig % BASICCALCULATOR, by itself, creates a new BASICCALCULATOR or raises the existing % singleton*.
%
% H = BASICCALCULATOR returns the handle to a new BASICCALCULATOR or the handle to % the existing singleton*.
%
% BASICCALCULATOR('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in BASICCALCULATOR.M with the given input arguments.
%
% BASICCALCULATOR('Property','Value',...) creates a new BASICCALCULATOR or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before BasicCalculator_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to BasicCalculator_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 BasicCalculator
% Last Modified by GUIDE v2.5 19-Sep-2011 10:40:02
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @BasicCalculator_OpeningFcn, ...
'gui_OutputFcn', @BasicCalculator_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
ifnargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
% --- Executes just before BasicCalculator is made visible. function BasicCalculator_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 BasicCalculator (see VARARGIN)
% Choose default command line output for BasicCalculator
handles.output = hObject;
% UIWAIT makes BasicCalculator wait for user response (see UIRESUME) % uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line. functionvarargout = BasicCalculator_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;
function edit1_zahl1_Callback(hObject, eventdata, handles) % hObject handle to edit1_zahl1 (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 edit1_zahl1 as text % str2double(get(hObject,'String')) returns contents of edit1_zahl1 as a double
% --- Executes during object creation, after setting all properties. function edit1_zahl1_CreateFcn(hObject, eventdata, handles) % hObject handle to edit1_zahl1 (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. ifispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');
end
function edit2_zahl2_Callback(hObject, eventdata, handles) % hObject handle to edit2_zahl2 (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 edit2_zahl2 as text % str2double(get(hObject,'String')) returns contents of edit2_zahl2 as a double
% --- Executes during object creation, after setting all properties. function edit2_zahl2_CreateFcn(hObject, eventdata, handles) % hObject handle to edit2_zahl2 (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. ifispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');
end
function edit3_Ergebnis_Callback(hObject, eventdata, handles) % hObject handle to edit3_Ergebnis (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 edit3_Ergebnis as text % str2double(get(hObject,'String')) returns contents of edit3_Ergebnis as a double
% --- Executes during object creation, after setting all properties. function edit3_Ergebnis_CreateFcn(hObject, eventdata, handles) % hObject handle to edit3_Ergebnis (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. ifispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton1_plus. function pushbutton1_plus_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1_plus (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) clc;
a_sum=get(handles.edit1_zahl1,'String');
b_sum=get(handles.edit2_zahl2,'String');
total_sum=str2num(a_sum)+str2num(b_sum);
c_sum=num2str(total_sum);
set(handles.edit3_Ergebnis,'String',c_sum);
guidata(hObject, handles);
% --- Executes on button press in pushbutton2_minus. function pushbutton2_minus_Callback(hObject, eventdata, handles) % hObject handle to pushbutton2_minus (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) clc;
a_min=get(handles.edit1_zahl1, 'String');
b_min=get(handles.edit2_zahl2,'String');
total_min=str2num(a_min)-str2num(b_min);
c_min=num2str(total_min);
set(handles.edit3_Ergebnis,'String',c_min);
guidata(hObject, handles);
% --- Executes on button press in pushbutton3_mal. function pushbutton3_mal_Callback(hObject, eventdata, handles) % hObject handle to pushbutton3_mal (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) clc;
a_mal=get(handles.edit1_zahl1,'String');
b_mal=get(handles.edit2_zahl2,'String');
total_mal=str2num(a_mal)*str2num(b_mal);
c_mal=num2str(total_mal);
set(handles.edit3_Ergebnis,'String',c_mal);
guidata(hObject, handles);
% --- Executes on button press in pushbutton4_geteilt. function pushbutton4_geteilt_Callback(hObject, eventdata, handles) % hObject handle to pushbutton4_geteilt (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
a_div=get(handles.edit1_zahl1,'String');
b_div=get(handles.edit2_zahl2,'String');
total_div=str2num(a_div)/str2num(b_div);
c_div=num2str(total_div);
set(handles.edit3_Ergebnis,'String',c_div);
guidata(hObject, handles);
% --- Executes on button press in pushbutton5_ASCII_File_erstellen. function pushbutton5_ASCII_File_erstellen_Callback(hObject, eventdata, handles) % hObject handle to pushbutton5_ASCII_File_erstellen (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% A=rand(5,6); %Beispielmatrix dlmwrite('zufall.asc',[a_sum,b_sum],'delimiter',';','precision','%10.6f');
% Speichern der Werte a und b im file: zufall.asc % die Endung .asc dient zum speichern im ASCII Format. anstelle von % [a,b] % kann auch A geschrieben werden, dann wird die MAtrix A ins File % übernommen guidata(hObject, handles);
% --- Executes on button press in pushbutton6_Quit. function pushbutton6_Quit_Callback(hObject, eventdata, handles) % hObject handle to pushbutton6_Quit (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
ok, ich habs gelöst, über die handles kann ich innerhalb einer funktion meine Variablen weitergeben und in neue funktionen übernehmen.
Danke euch trotzdem für die Hilfe.
Einstellungen und Berechtigungen
Du kannst Beiträge in dieses Forum schreiben. Du kannst auf Beiträge in diesem Forum antworten. Du kannst deine Beiträge in diesem Forum nicht bearbeiten. Du kannst deine Beiträge in diesem Forum nicht löschen. Du kannst an Umfragen in diesem Forum nicht mitmachen. Du kannst Dateien in diesem Forum posten Du kannst Dateien in diesem Forum herunterladen
MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.