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

Plot in Funktion übergeben

 

Florre
Forum-Fortgeschrittener

Forum-Fortgeschrittener


Beiträge: 60
Anmeldedatum: 28.02.13
Wohnort: ---
Version: R2009a, R2016b
     Beitrag Verfasst am: 18.03.2013, 11:51     Titel: Plot in Funktion übergeben
  Antworten mit Zitat      
Hallo Liebe MATLAB-Gemeinde,
Ich hab ein kleines Problem.
Ich erkläre das mal kurz und poste dann meine Codes.

Also ich hab eine kleine GUI mit guide gebastelt...
In dieser GUI kann man einiges auswählen und dann kann man ein Programm starten. Alles ohne prob.
Jetzt soll folgenes passieren.
Meine aufgerufene Funktion rechnet einwenig.. auch ohne Probleme.
Meine eigene Funktion ruft jedenfalls eine weitere Funktion auf die ich im I-net gefunden habe...
In dieser Funktion wird etwas geplotet.. Ich möchte aber gerne das die plots nicht auf meinem Bildschirm auftauchen... da sie in der GUI geplotet werden sollen.

ich weiß mit:
Code:
figure('visibile','off');
zeigt er mir das nicht an.

Ich will die plots aber unter einen bestimmten Namen speichern wie bekomm ich das hin, denn wenn ich den obenstehenden Code verwende und ich das bild wieder mit gcf aufrufe macht er eine neue Figure.

Ich poste mal meine Codes:

Das aus dem I-net:

Code:
function varargout = peakfinder(x0, sel, thresh, extrema)
%PEAKFINDER Noise tolerant fast peak finding algorithm
%   INPUTS:
%       x0 - A real vector from the maxima will be found (required)
%       sel - The amount above surrounding data for a peak to be
%           identified (default = (max(x0)-min(x0))/4). Larger values mean
%           the algorithm is more selective in finding peaks.
%       thresh - A threshold value which peaks must be larger than to be
%           maxima or smaller than to be minima.
%       extrema - 1 if maxima are desired, -1 if minima are desired
%           (default = maxima, 1)
%   OUTPUTS:
%       peakLoc - The indicies of the identified peaks in x0
%       peakMag - The magnitude of the identified peaks
%
%   [peakLoc] = peakfinder(x0) returns the indicies of local maxima that
%       are at least 1/4 the range of the data above surrounding data.
%
%   [peakLoc] = peakfinder(x0,sel) returns the indicies of local maxima
%       that are at least sel above surrounding data.
%
%   [peakLoc] = peakfinder(x0,sel,thresh) returns the indicies of local
%       maxima that are at least sel above surrounding data and larger
%       (smaller) than thresh if you are finding maxima (minima).
%
%   [peakLoc] = peakfinder(x0,sel,thresh,extrema) returns the maxima of the
%       data if extrema > 0 and the minima of the data if extrema < 0
%
%   [peakLoc, peakMag] = peakfinder(x0,...) returns the indicies of the
%       local maxima as well as the magnitudes of those maxima
%
%   If called with no output the identified maxima will be plotted along
%       with the input data.
%
%   Note: If repeated values are found the first is identified as the peak
%
% Ex:
% t = 0:.0001:10;
% x = 12*sin(10*2*pi*t)-3*sin(.1*2*pi*t)+randn(1,numel(t));
% x(1250:1255) = max(x);
% peakfinder(x)
%
% Copyright Nathanael C. Yoder 2011 (nyoder@gmail.com)

% Perform error checking and set defaults if not passed in
error(nargchk(1,4,nargin,'struct'));
error(nargoutchk(0,2,nargout,'struct'));

s = size(x0);
flipData =  s(1) < s(2);
len0 = numel(x0);
if len0 ~= s(1) && len0 ~= s(2)
    error('PEAKFINDER:Input','The input data must be a vector')
elseif isempty(x0)
    varargout = {[],[]};
    return;
end
if ~isreal(x0)
    warning('PEAKFINDER:NotReal','Absolute value of data will be used')
    x0 = abs(x0);
end

if nargin < 2 || isempty(sel)
    sel = (max(x0)-min(x0))/4;
elseif ~isnumeric(sel) || ~isreal(sel)
    sel = (max(x0)-min(x0))/4;
    warning('PEAKFINDER:InvalidSel',...
        'The selectivity must be a real scalar.  A selectivity of %.4g will be used',sel)
elseif numel(sel) > 1
    warning('PEAKFINDER:InvalidSel',...
        'The selectivity must be a scalar.  The first selectivity value in the vector will be used.')
    sel = sel(1);
end

if nargin < 3 || isempty(thresh)
    thresh = [];
elseif ~isnumeric(thresh) || ~isreal(thresh)
    thresh = [];
    warning('PEAKFINDER:InvalidThreshold',...
        'The threshold must be a real scalar. No threshold will be used.')
elseif numel(thresh) > 1
    thresh = thresh(1);
    warning('PEAKFINDER:InvalidThreshold',...
        'The threshold must be a scalar.  The first threshold value in the vector will be used.')
end

if nargin < 4 || isempty(extrema)
    extrema = 1;
else
    extrema = sign(extrema(1)); % Should only be 1 or -1 but make sure
    if extrema == 0
        error('PEAKFINDER:ZeroMaxima','Either 1 (for maxima) or -1 (for minima) must be input for extrema');
    end
end

x0 = extrema*x0(:); % Make it so we are finding maxima regardless
thresh = thresh*extrema; % Adjust threshold according to extrema.
dx0 = diff(x0); % Find derivative
dx0(dx0 == 0) = -eps; % This is so we find the first of repeated values
ind = find(dx0(1:end-1).*dx0(2:end) < 0)+1; % Find where the derivative changes sign

% Include endpoints in potential peaks and valleys
x = [x0(1);x0(ind);x0(end)];
ind = [1;ind;len0];

% x only has the peaks, valleys, and endpoints
len = numel(x);
minMag = min(x);


if len > 2 % Function with peaks and valleys
   
    % Set initial parameters for loop
    tempMag = minMag;
    foundPeak = false;
    leftMin = minMag;
   
    % Deal with first point a little differently since tacked it on
    % Calculate the sign of the derivative since we taked the first point
    %  on it does not neccessarily alternate like the rest.
    signDx = sign(diff(x(1:3)));
    if signDx(1) <= 0 % The first point is larger or equal to the second
        ii = 0;
        if signDx(1) == signDx(2) % Want alternating signs
            x(2) = [];
            ind(2) = [];
            len = len-1;
        end
    else % First point is smaller than the second
        ii = 1;
        if signDx(1) == signDx(2) % Want alternating signs
            x(1) = [];
            ind(1) = [];
            len = len-1;
        end
    end
   
    % Preallocate max number of maxima
    maxPeaks = ceil(len/2);
    peakLoc = zeros(maxPeaks,1);
    peakMag = zeros(maxPeaks,1);
    cInd = 1;
    % Loop through extrema which should be peaks and then valleys
    while ii < len
        ii = ii+1; % This is a peak
        % Reset peak finding if we had a peak and the next peak is bigger
        %   than the last or the left min was small enough to reset.
        if foundPeak
            tempMag = minMag;
            foundPeak = false;
        end
       
        % Make sure we don't iterate past the length of our vector
        if ii == len
            break; % We assign the last point differently out of the loop
        end
       
        % Found new peak that was lager than temp mag and selectivity larger
        %   than the minimum to its left.
        if x(ii) > tempMag && x(ii) > leftMin + sel
            tempLoc = ii;
            tempMag = x(ii);
        end
       
        ii = ii+1; % Move onto the valley
        % Come down at least sel from peak
        if ~foundPeak && tempMag > sel + x(ii)
            foundPeak = true; % We have found a peak
            leftMin = x(ii);
            peakLoc(cInd) = tempLoc; % Add peak to index
            peakMag(cInd) = tempMag;
            cInd = cInd+1;
        elseif x(ii) < leftMin % New left minima
            leftMin = x(ii);
        end
    end
   
    % Check end point
    if x(end) > tempMag && x(end) > leftMin + sel
        peakLoc(cInd) = len;
        peakMag(cInd) = x(end);
        cInd = cInd + 1;
    elseif ~foundPeak && tempMag > minMag % Check if we still need to add the last point
        peakLoc(cInd) = tempLoc;
        peakMag(cInd) = tempMag;
        cInd = cInd + 1;
    end
   
    % Create output
    peakInds = ind(peakLoc(1:cInd-1));
    peakMags = peakMag(1:cInd-1);
else % This is a monotone function where an endpoint is the only peak
    [peakMags,xInd] = max(x);
    if peakMags > minMag + sel
        peakInds = ind(xInd);
    else
        peakMags = [];
        peakInds = [];
    end
end

% Apply threshold value.  Since always finding maxima it will always be
%   larger than the thresh.
if ~isempty(thresh)
    m = peakMags>thresh;
    peakInds = peakInds(m);
    peakMags = peakMags(m);
end



% Rotate data if needed
if flipData
    peakMags = peakMags.';
    peakInds = peakInds.';
end



% Change sign of data if was finding minima
if extrema < 0
    peakMags = -peakMags;
    x0 = -x0;
end
% Plot if no output desired
if nargout == 0
    if isempty(peakInds)
        disp('No significant peaks found')
    else
        figure;
        plot(1:len0,x0,'.-',peakInds,peakMags,'ro','linewidth',2);
        set(gcf,'Position',[0,0,1500,1300]); %Größe Einstellen
    end
else
    varargout = {peakInds,peakMags};
end


Mein Eigenes Programm:
Code:
function Auswertung(Ordner,Auswertung)
%format('long');                                                          
for Linkanzahl=1:size(Ordner,1)
    dateien=Ordner{Linkanzahl,1};
                                                 
cd(dateien);                                                              
[stat,Text]=fileattrib('*.txt');                                            
names={Text.Name};                                                          

x=1;                                                                        
    while(1)
    File=names{x};                                                       %Dateien einzeln nehmen
    comma2point(File)                                                       %extra Funktion aus dem I-net gefischt und verändert Ersetzt die Kommas durch Punkte MATLAB braucht Punkte
    Einlesen=dlmread(File,'\t',8,0);                                        %Da die Dateien Einen Text Kopf und auch die Spalten Text enthalten und MATLAB nicht viel damit anfagen kann werden sie Rausgefilter (Nicht eingelesen)
    Einlesen(:,4)=Einlesen(:,4).*1000;
    Einlesen(:,6)=Einlesen(:,2).*Einlesen(:,3);                            
    Einlesen(:,7)=Einlesen(:,4).*Einlesen(:,5);                                
       
 
    S1=zeros(1,(size(Einlesen,1))-1);                                                                
   
        for y = 2:(size(Einlesen,1))-1
       S1(y)=(Einlesen(y,1)-Einlesen(y-1,1))*((Einlesen(y-1,6)+Einlesen(y,6))/2);  
        end
 
     Sa=zeros(1,size(Einlesen,1));                                                                
     
        for g=2:length(S1)
       Sa(g)=Sa(g-1)+S1(g);
        end
   Sa(g+1)=Sa(g);
   
    S2=zeros(1,(size(Einlesen,1))-1);

        for y = 2:(size(Einlesen,1))-1
        S2(y)=(Einlesen(y,1)-Einlesen(y-1,1))*((Einlesen(y-1,7)+Einlesen(y,7))/2);
        end
   
    Sb=zeros(1,size(Einlesen,1));
   
        for g=2:length(S2)
       Sb(g)=Sb(g-1)+S2(g);
        end
   Sb(g+1)=Sb(g);
        % Ende von Esekundär
   
         M=Sb./Sa;                                                          
    Einlesen(:,8)=Sa';
    Einlesen(:,9)=Sb';
    Einlesen(:,10)=M';                                                      
    Dokument=sprintf('%s%s',names{x},'_neu.txt');
    %Speichern der Datei beginnt hier
    fid=fopen(Dokument,'wt');
    fprintf(fid,'%s\t','Time','Primaerspannung','Primaerstrom','Sekundaerspannung', ...
        'Sekundaerstrom','Primaerleistung','Sekundaerleistung','Primaerenergie','Sekundaerenergie','Wirkungsgrad');
     fprintf(fid,'\n');
     fprintf(fid,'%s\t','s','V','A','V','A','W','W','Ws','Ws','%');
     fprintf(fid,'\n');
    fclose(fid);
    dlmwrite(Dokument,Einlesen,'delimiter', '\t','-append');                
    point2comma(Dokument);
   
   Punkte=peakfinder(Einlesen(:,5));
   peakfinder(Einlesen(:,5));
   hallo=gcf;
   Bildname1=sprintf('%s%s',names{x},'_1.bmp');   %Plot speichern
    saveas(hallo,Bildname1,'bmp');
    close Figure 1;
   
    if Auswertung==1
    Endspalte=peakfinder(Einlesen(:,10));
    Wirkung=Einlesen(Endspalte,10)*100;
    Endzeit=Einlesen(Endspalte,1);
    Durchschlagszeitp=Einlesen(Punkte,1);
    Durchschlagszahl=length(Punkte);
    Brennzeiteinzel=diff(Durchschlagszeitp);
    BrennzeitFunken=[Brennzeiteinzel;(Endzeit-Durchschlagszeitp(end))];
    FunkenZeit=sum(BrennzeitFunken);
    Imax(1:length(Durchschlagszeitp))=Einlesen(Punkte,5);
    Excelkopf={'Durchschlagzahl','Brennzeit','max. Wirkungsgrad','Durchschlag','Durchschlagszeit','Imax','t zu Imax'};
    ExcelKoerper1=[Durchschlagszahl,FunkenZeit,Wirkung];
    ExcelKoerper2=[(1:length(BrennzeitFunken))',BrennzeitFunken,Imax',Durchschlagszeitp];
    xlswrite([File '.xls'],Excelkopf,'Tabelle1','A1');
    xlswrite([File '.xls'],ExcelKoerper1,'Tabelle1','A2');
    xlswrite([File '.xls'],ExcelKoerper2,'Tabelle1','D2');
    else
        Endspalte=peakfinder(Einlesen(:,10));
    end
                x=x+1;
        if (x>length(names)) ,break,end
    end
end
 

Ich hoffe mir kann jemand Helfen...
Private Nachricht senden Benutzer-Profile anzeigen


Seban
Forum-Meister

Forum-Meister


Beiträge: 600
Anmeldedatum: 19.01.12
Wohnort: ---
Version: ab R2014b
     Beitrag Verfasst am: 18.03.2013, 12:23     Titel: Re: Plot in Funktion übergeben
  Antworten mit Zitat      
Hallo Florre,

Florre hat Folgendes geschrieben:
Ich möchte aber gerne das die plots nicht auf meinem Bildschirm auftauchen... da sie in der GUI geplotet werden sollen.

Wenn ich mich nicht täusche, musst du einfach den handle der axes in die geplottet werden soll an die Funktion plot übergeben.

Florre hat Folgendes geschrieben:
Ich will die plots aber unter einen bestimmten Namen speichern wie bekomm ich das hin, denn wenn ich den obenstehenden Code verwende und ich das bild wieder mit gcf aufrufe macht er eine neue Figure.

Wo hast du oben stehenden Code denn eingefügt? Vor oder nach dem Plot-Aufruf? Sollte davor geschehen

Grüße,
Seban
Private Nachricht senden Benutzer-Profile anzeigen
 
Florre
Themenstarter

Forum-Fortgeschrittener

Forum-Fortgeschrittener


Beiträge: 60
Anmeldedatum: 28.02.13
Wohnort: ---
Version: R2009a, R2016b
     Beitrag Verfasst am: 18.03.2013, 12:40     Titel:
  Antworten mit Zitat      
Hallo Seban,

Also in meinem Programm:

Code:
Punkte=peakfinder(Einlesen(:,5));
   peakfinder(Einlesen(:,5));
   hallo=gcf;
   Bildname1=sprintf('%s%s',names{x},'_1.bmp');   %Plot speichern
    saveas(hallo,Bildname1,'bmp');
    close Figure 1;
 

Das ist zum speichern..

und das mit dem Plotten...
Code:
       figure;
        plot(1:len0,x0,'.-',peakInds,peakMags,'ro','linewidth',2);
        set(gcf,'Position',[0,0,1500,1300]); %Größe Einstellen
 


hatte mal
Code:
figure('visible','off')


naja wenn ich das so lasse öffnet er ja den plot...
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.