如何在循环中运行不同的函数

时间:2018-04-19 14:29:53

标签: matlab function nested-loops matlab-guide

function numbersensors_Callback(hObject, eventdata, handles) // edit box to enter the number of sensors
    dat=str2double(get(hObject,'string'));
    newcell = cell(dat,3);
    newcell(:) = {''};
    handles.rnames=cell(1,dat);
    assignin('base','rnames',handles.rnames);
    for i=1:dat
       handles.rnames{i} = strcat('S',num2str(i));
    set(handles.uitable2, 'data',newcell,'RowName',handles.rnames);
    end
    msgbox({'Sensor Connection order';'Light';'Pressure';'Humidity';'Temperature'});
    guidata(hObject, handles);

 输入num_sensors后。表调整uitable的大小.UID是获取相应传感器输出的函数的输入     function plot_on_Callback(hObject,eventdata,handles)//切换按钮以运行函数

tdata = get(handles.uitable2, 'data');
uid=tdata(:,1:2);
assignin('base', 'uid', uid);
y_fp =tdata(:,3);

col=uid((contains(uid(:,2),'Light'))),1);
ill=NaN(100,length(col));
pre=[];
hum=[];
tem=[];
tme=NaN(100,length(uid));
 cnt=0;
   while get(hObject,'value')
         set(hObject,'String','STOP');
   for k=1:length(uid)
           cur=uid{k};
           cnt=cnt+1;
           tme(:,k)=[tme(2:end,k);cnt];
           assignin('base','y',tme);  

        if strcmp(uid((contains(uid(:,2),'Temperature'))),cur)==1         
           temp= get_temperature(cur);
           tem=[tem;temp]; 

    elseif strcmp(uid((contains(uid(:,2),'Pressure'))),cur)==1                   
           press=get_pressure(cur);
           pre=[pre;press];

    elseif strcmp(uid((contains(uid(:,2),'Humidity'))),cur)==1                
           humi= get_humidity(cur);
           hum=[hum;humi];
           assignin('base','hum',hum);   

        else            
           illu=get_illuminance(cur);
           ill(:,k)=[ill(2:end,k);illu]; 
           assignin('base','x',ill);    
           break;
        end    

         plot(handles.axes2,ill(:,1:end));
         legend(handles.axes2,handles.rnames);
         warning('off');
         xlabel(handles.axes2,'Time(sec)');
         ylabel(handles.axes2,'Sensors(Lux)'); 
         pause(1); 

   end

   end          


set(hObject,'String','START')
guidata(hObject,handles);


function p= get_pressure(XYZ) %% Similar functions for get_temperature and get_humidity. din't mention here to save the space
    import com.tinkerforge.IPConnection;
    import com.tinkerforge.BrickletBarometer;
    HOST = 'localhost';
    PORT = 4223;
    UID = XYZ; % Change XYZ to the UID of your Ambient Light Bricklet 2.0
    ipcon = IPConnection(); % Create IP connection
    pr = handle(BrickletBarometer(UID, ipcon), 'CallbackProperties'); 
    ipcon.connect(HOST, PORT); % Connect to brickd
    p= pr.getAirPressure()/1000;
    ipcon.disconnect();

function x= get_illuminance(XYZ)
    import com.tinkerforge.IPConnection;
    import com.tinkerforge.BrickletAmbientLightV2;  
    HOST = 'localhost';
    PORT = 4223;
    UID = XYZ; % Change XYZ to the UID of your Ambient Light Bricklet 2.0
    ipcon = IPConnection(); % Create IP connection
    al = handle(BrickletAmbientLightV2(UID, ipcon), 'CallbackProperties'); % Create device object
    ipcon.connect(HOST, PORT); % Connect to brickd
    x = al.getIlluminance()/100; 
    al.setConfiguration(6,2);
    ipcon.disconnect();

为了开发一个用于从不同传感器进行实时数据记录的GUI,我创建了一个切换按钮" plot"绘制数据。因为,我使用外部供应商微控制器和功能来使用MatLab收集数据。函数输出是标量。所以,我想在循环中运行函数并将值获取到矩阵中。但我的代码未能贯穿uid的长度。那么,如何修复呢?或欢迎任何有效阅读数据的建议。谢谢

0 个答案:

没有答案