⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mxa gui example.m

📁 Agilent 仪器与MATLAB互联
💻 M
字号:
function example10
% MATLAB/MXA example 10
% % GUI Matlab demo for the MXA

% Version: 1.0
% Date: Sep 11, 2006
% 2006 Agilent Technologies, Inc.


% Main parameter initialization

period=0.1; % timer period for display update

% Clean up any unclosed instrument object
oldobjs=instrfind;
if ~isempty(oldobjs)
    disp('Cleaning up ...')
    delete(oldobjs);
    clear oldobjs;
end

% host names

localhost = 'localhost';

% device driver initialization

disp(' ')
disp('Connecting to MXA ...');
mxa=tcpip(localhost,5025);
set(mxa,'InputBufferSize',30000);
set(mxa,'Timeout',10);
fopen(mxa);

% Intrument initial setup

fprintf(mxa,':INST:NSEL 1'); % SA mode
fprintf(mxa,'FORM:DATA REAL,32'); % data in real format, 32 bits
nr_points = str2double(query(mxa,'SWE:POIN?')); % number of trace points
ref = str2double(query(mxa,'DISP:WIND:TRAC:Y:RLEV?')); % reference level
span = str2double(query(mxa,':FREQ:SPAN?')); % span
center = str2double(query(mxa,':FREQ:CENT?')); % center frequency
aver = str2double(query(mxa,':AVER?')); % average mode
fprintf(mxa,':DISP:ENAB OFF'); % disable GPSA display for faster processing


% GUI layout

axes_color = [.6 .6 .6]; % RGB components
fh=figure(1);
clf
set(fh,'Units','normalized',...
    'Backingstore','off',...
    'Color','k',...
    'NumberTitle','off',...
    'Menubar','none',...
    'Name','Spectrum Analizer Demo',...
    'Position',[0    0    1.0000    0.975],...
    'DefaultAxesFontSize',12,...
    'DefaultTextFontSize',14,...
    'DefaultTextFontName','Agilent Century ITC TT',...
    'DefaultTextColor', [1 1 1],...
    'DefaultAxesColor','k',...
    'DefaultAxesXColor', axes_color,...
    'DefaultAxesYColor', axes_color,...
    'DefaultAxesZColor', axes_color,...
    'DefaultAxesPosition',[.07 .15 .78 .75],...
    'DefaultAxesUnits','pixels',...
    'DefaultAxesBox','on',...
    'KeyPressFcn',@keypress,...
    'DefaultUicontrolFontsize',16,...
    'DefaultUicontrolFontName','Agilent Century ITC TT',...
    'DefaultUicontrolKeyPressFcn',@keypress);

ah=axes('Nextplot','add',...
    'gridlinestyle','-',...
    'XGrid','on',...
    'YGrid','on',...
    'FontName','Agilent Century ITC TT',...
    'XTickLabel',[],...
    'XLim',[1 nr_points],...
    'Xtick',(nr_points-1)/10:(nr_points-1)/10:nr_points-1);

ph = plot(ah,zeros(1,nr_points),'y');

freq_text_h = text('Units', 'normalized',...
    'FontWeight','bold',...
    'Position', [-.05 -.05],...
    'Color','g',...
    'String', 'Center:');

span_text_h = text('Units', 'normalized',...
    'FontWeight','bold',...
    'Position', [.73 -.05],...
    'String', 'Span:');

ref_text_h = text('Units', 'normalized',...
    'FontWeight','bold',...
    'Position', [.03 1.05],...
    'String', 'Ref:');

% softkey_pannel initialization
softkey1h = uicontrol( 'style', 'pushbutton',...
    'string', 'Cont',...
    'Position',[880 630 130 60],...
    'callback', @(obj,eventData) cont_button_callback(obj));
softkey2h = uicontrol( 'style', 'pushbutton',...
    'string', 'Single',...
    'Position',[880 532 130 60],...
    'callback', @stop_button_callback);
softkey3h = uicontrol( 'style', 'togglebutton',...
    'string', 'Averaging',...
    'Position', [880 434 130 60],...
    'Value', aver,...
    'callback', @ave_button_callback);
softkey7h = uicontrol( 'style', 'pushbutton',...
    'string', 'Close',...
    'position',[880 42 130 60],...
    'callback', @close_button_callback);

% timer for plot updates
th =timer('timerfcn',@update_plot,...
    'ExecutionMode','fixedrate',...
    'Period',period);

% Start the plot updates
start(th)

% callback and utility functions

% function to terminate the Matlab application
    function close_button_callback(source,eventdata)
        stop(th)
        fprintf(mxa,':DISP:ENAB ON');
        disp('Disconnecting from MXA ...');
        fclose(mxa);
        delete(mxa);
        clear mxa;
        close(fh)
    end

% function to turn on the plot continuous mode
    function cont_button_callback(source,eventdata)
        stop(th)
        set(th,'ExecutionMode','fixedrate')
        start(th)
    end

% function to activate a single plot sweep
    function stop_button_callback(source,eventdata)
        stop(th)
        set(th,'ExecutionMode','singleShot')
        start(th)
    end

% function to process the Average toggle button
    function ave_button_callback(source,eventdata)
        if strcmp(eventdata,'key')
            if ~get(softkey3h,'Value')
                set(softkey3h,'Value',1)
            else
                set(softkey3h,'Value',0)
            end
        end
        button_status=get(source,'value');

        if button_status
            fprintf(mxa,':AVER ON');

        else
            fprintf(mxa,':AVER OFF');

        end
    end

% function that processes key press events
    function keypress(source,event)
        if (length(event.Modifier) == 2 && ...
                strcmp(event.Modifier{1},'shift') && ...
                strcmp(event.Modifier{2},'control'))

            if  strcmp(event.Key,'f2')
                feval(get(softkey1h,'Callback'),softkey1h,[])
            end
            if  strcmp(event.Key,'f3')
                feval(get(softkey2h,'Callback'),softkey2h,[])
            end
            if  strcmp(event.Key,'f4')
                feval(get(softkey3h,'Callback'),softkey3h,'key')
            end
            if strcmp(event.Key,'f8')
                feval(get(softkey7h,'Callback'),softkey7h,[])
            end

            if  strcmp(event.Key,'f')
                set(freq_text_h,'Color','g')
                set(span_text_h,'Color','w')
                set(ref_text_h,'Color','w')
            end
            if  strcmp(event.Key,'s')
                set(freq_text_h,'Color','w')
                set(span_text_h,'Color','g')
                set(ref_text_h,'Color','w')
            end
            if  strcmp(event.Key,'a')
                set(freq_text_h,'Color','w')
                set(span_text_h,'Color','w')
                set(ref_text_h,'Color','g')
            end
        end

    end

% function that processes frequency units
    function str = process_freq_units(val)

        if     (abs(val) < 1e3) str = sprintf('%3d Hz',val);
        elseif (abs(val) < 1e6) str = sprintf('%3.3f kHz',val/1e3);
        elseif (abs(val) < 1e9) str = sprintf('%3.6f MHz',val/1e6);
        else str = sprintf('%3.9f GHz',val/1e9);
        end
    end

    function update_plot(varargin)
        fprintf(mxa,'TRAC? TRACE1');
        data = binblockread(mxa,'float32');
        fscanf(mxa); % remove the data block terminator

        set(ph,'Ydata',data);

        ref = str2double(query(mxa,'DISP:WIND:TRAC:Y:RLEV?'));
        span = str2double(query(mxa,':FREQ:SPAN?'));
        center = str2double(query(mxa,':FREQ:CENT?'));

        rl=round(ref*10)/10;
        set(ah,'Ylim', [rl-100 rl])
        set(ah,'YTick',rl-100:10:rl)
        set(ah,'Yticklabel', rl-100:10:rl)

        set(freq_text_h, 'String', ['Center: ' process_freq_units(center)]);
        set(span_text_h, 'String', ['Span: ' process_freq_units(span)]);
        set(ref_text_h, 'String', sprintf('Ref: %.2f dBm', ref));

        drawnow
    end

 end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -