Advertisement
Ladislav

Function example #2

Apr 25th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.99 KB | None | 0 0
  1. function [ varargout ] = funkce2( varargin )
  2. %funkce2 Nacte data ze souboru a/nebo zobrazi grafy.
  3. %   [f, Zin] = funkce2(fileName)
  4. %       Nacte data ze souboru, ulozi je do vystupnich parametru a zobrazi
  5. %       grafy.
  6. %
  7. %   funkce2(f, Zin, isGrid, lineWidth)
  8. %       Zobrazi grafy pro zadane vstupni vektory f a Zin. Pokud je isGrid
  9. %       true nebo neni zadano, je v grafech zobrazena mrizka. Grafy jsou
  10. %       vykresleny s tloustkou cary lineWidth, pokud neni zadano, vychozi
  11. %       hodnota je 2px.
  12. %
  13.  
  14.     function [f, Zin, R] = readFile(fileName)
  15.         if (exist(fileName, 'file') == 0)
  16.             error('Soubor neexistuje.');
  17.         end;
  18.  
  19.         S11 = xlsread(fileName);
  20.         if (length(S11) > 5000)
  21.             warning('myApp:longFile', 'Soubor je delší než 5000 řádků.');
  22.         end;
  23.         S11 = [S11(:,1), S11(:,2) .* exp(1i * S11(:,3) / 180 * pi)];
  24.  
  25.         Zin = Z0 .* ((1 + S11(:,2)) ./ (1 - S11(:,2)));
  26.         f = S11(:,1);
  27.         R = abs(S11(:,2));
  28.     end
  29.    
  30.     function drawZPlot(f, Zin, isGrid, lineWidth)
  31.         figure('Name', 'Vstupní impedance');
  32.         plot(f, real(Zin), 'Color', 'blue', 'LineWidth', lineWidth, 'DisplayName', 'Real part');
  33.         hold on;
  34.         plot(f, imag(Zin), 'Color', 'green', 'LineWidth', lineWidth, 'DisplayName', 'Imaginary part');
  35.         title('Vstupní impedance');
  36.         xlabel('f [GHz]');
  37.         ylabel('Zin [\Omega]');
  38.         legend show;
  39.         if(isGrid)
  40.             grid on;
  41.         end;
  42.     end
  43.  
  44.     function drawRPlot(f, R, isGrid, lineWidth)
  45.         figure('Name', 'Koeficient odrazu');
  46.         plot(f, R, 'Color', 'blue', 'LineWidth', lineWidth);
  47.         title('Koeficient odrazu');
  48.         xlabel('f [GHz]');
  49.         ylabel('|S11|');
  50.         if(isGrid)
  51.             grid on;
  52.         end;
  53.     end
  54.  
  55. Z0 = 50;
  56. profile clear;
  57. profile on;
  58. tic;
  59.  
  60. switch (nargin)
  61.     case 1
  62.         fileName = varargin{1};
  63.         isGrid = true;
  64.         lineWidth = 2;
  65.        
  66.         [f, Zin, R] = readFile(fileName);
  67.         drawZPlot(f, Zin, isGrid, lineWidth);
  68.         drawRPlot(f, R, isGrid, lineWidth);
  69.        
  70.         if (nargout >= 2)
  71.             varargout{1} = f;
  72.             varargout{2} = Zin;
  73.         end;
  74.        
  75.     case {2, 3, 4}
  76.         f = varargin{1};
  77.         Zin = varargin{2};
  78.         if (nargin >= 3)
  79.             isGrid = varargin{3};
  80.         else
  81.             isGrid = true;
  82.         end;
  83.         if (nargin >= 4)
  84.             lineWidth = varargin{4};
  85.         else
  86.             lineWidth = 2;
  87.         end;
  88.        
  89.         if (length(f) ~= length(Zin))
  90.             error('Vstupni argumenty nemaji stejnou delku.');
  91.         end;
  92.        
  93.         R = abs((Zin - Z0) ./ (Zin + Z0));
  94.  
  95.         drawZPlot(f, Zin, isGrid, lineWidth);
  96.         drawRPlot(f, R, isGrid, lineWidth);
  97.  
  98.         varargout = [];
  99.        
  100.     otherwise
  101.         error('Neplatny pocet vstupnich parametru.');
  102. end;
  103.    
  104. fprintf('Funkce bezela %f sekund.\n', toc());
  105. profile off;
  106. profile report;
  107.  
  108. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement