WICHTIG: Der Betrieb von goMatlab.de wird privat finanziert fortgesetzt. - Mehr Infos...

Mein MATLAB Forum - goMatlab.de

Mein MATLAB Forum

 
Gast > Registrieren       Autologin?   

Partner:




Forum
      Option
[Erweitert]
  • Diese Seite per Mail weiterempfehlen
     


Gehe zu:  
Neues Thema eröffnen Neue Antwort erstellen

Variablen aus Funktion auslesen

 

morisq
Forum-Century

Forum-Century


Beiträge: 140
Anmeldedatum: 24.06.11
Wohnort: ---
Version: R2010a
     Beitrag Verfasst am: 19.09.2011, 09:28     Titel: Variablen aus Funktion auslesen
  Antworten mit Zitat      
Hallo,
ich arbeite erst seit kurzem mit Matlab und habe eine Frage die evtl. kein großes können vorraussetzt, mir dennoch Schwierigkeiten bereitet.

Ich habe innerhalb eines .m-Files Funktionen definiert in welchen Variablen mit Werten belegt werden.
Code:
function XY_Callback(hObject, eventdata, handles)
.........
........
var_a = Wert1;
var_b = Wert2;
.
.
.
 

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?

Vielen Dank schonmal
Private Nachricht senden Benutzer-Profile anzeigen


monica
Forum-Fortgeschrittener

Forum-Fortgeschrittener


Beiträge: 52
Anmeldedatum: 08.01.09
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 19.09.2011, 10:14     Titel:
  Antworten mit Zitat      
Code:


könnte helfen, siehe help.
Private Nachricht senden Benutzer-Profile anzeigen
 
Jan S
Moderator

Moderator


Beiträge: 11.057
Anmeldedatum: 08.07.10
Wohnort: Heidelberg
Version: 2009a, 2016b
     Beitrag Verfasst am: 19.09.2011, 10:31     Titel: Re: Variablen aus Funktion auslesen
  Antworten mit Zitat      
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.

Gruß, Jan
Private Nachricht senden Benutzer-Profile anzeigen
 
morisq
Themenstarter

Forum-Century

Forum-Century


Beiträge: 140
Anmeldedatum: 24.06.11
Wohnort: ---
Version: R2010a
     Beitrag Verfasst am: 19.09.2011, 10:31     Titel:
  Antworten mit Zitat      
meisnt du ich sollte innerhalb meiner funktion die variablen zunächst mit
Code:
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.
Private Nachricht senden Benutzer-Profile anzeigen
 
morisq
Themenstarter

Forum-Century

Forum-Century


Beiträge: 140
Anmeldedatum: 24.06.11
Wohnort: ---
Version: R2010a
     Beitrag Verfasst am: 19.09.2011, 10:33     Titel:
  Antworten mit Zitat      
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
Private Nachricht senden Benutzer-Profile anzeigen
 
morisq
Themenstarter

Forum-Century

Forum-Century


Beiträge: 140
Anmeldedatum: 24.06.11
Wohnort: ---
Version: R2010a
     Beitrag Verfasst am: 19.09.2011, 10:41     Titel:
  Antworten mit Zitat      
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:
function varargout = 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',   []);
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 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;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = 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.
if ispc && 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.
if ispc && 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.
if ispc && 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)

guidata(hObject, handles);
Private Nachricht senden Benutzer-Profile anzeigen
 
morisq
Themenstarter

Forum-Century

Forum-Century


Beiträge: 140
Anmeldedatum: 24.06.11
Wohnort: ---
Version: R2010a
     Beitrag Verfasst am: 19.09.2011, 11:01     Titel:
  Antworten mit Zitat      
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.
Private Nachricht senden Benutzer-Profile anzeigen
 
Neues Thema eröffnen Neue Antwort erstellen



Einstellungen und Berechtigungen
Beiträge der letzten Zeit anzeigen:

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
.





 Impressum  | Nutzungsbedingungen  | Datenschutz | FAQ | goMatlab RSS Button RSS

Hosted by:


Copyright © 2007 - 2025 goMatlab.de | Dies ist keine offizielle Website der Firma The Mathworks

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.