0

假设 vec_A、vec_B、vec_c 是一些带有随机数据的矩阵。我想每 5 分钟将数据写入文本文件,我的代码如下:

function samplegui_OpeningFcn(hObject, ~, handles, varargin)

handles.timer = timer(...
       'ExecutionMode', 'fixedRate', ...   % Run timer repeatedly
       'Period', 300, ...                % Initial period.
       'TimerFcn', {@open,hObject}); % Specify callback

handles.output = hObject;
handles.vec_A=[];
handles.vec_B=[];
handles.vec_C=[];
guidata(hObject, handles);

function open_Callback(hObject, eventdata, handles) % push button to receive serial data.

cnt=0;

while 1
       
       % Getting data  from Serial Port
        get_lines=fgets(handles.se) % getting data from serial port 
           if~isempty(get_lines)
            cnt=cnt+1;   
       if strfind(get_lines,'T')   %Parsing data
       handles.vec_A=[handles.vec_A;[timet newword]];
       plot(handles.vec_A(:,1),handles.vec_A(:,2:end),'r'); % plotting

       % Same follows for parsing and plot vec_B and Vec_C
       drawnow(); % to update the Plots
       end
     end
Pause(.05);
   
      start(handles.timer);  % saving the data
      dlmwrite('My_sample1.txt',handles.vec_A);
      dlmwrite('My_sample2.txt',handles.vec_B);
      dlmwrite('My_sample3.txt',handles.vec_C);
      stop(handles.timer);
end
guidata(hObject, handles);

运行我的代码时,出现以下错误:

为计时器“timer-6”评估 TimerFcn 时出错

输入参数过多。

在这种情况下如何执行计时器以每五分钟成功写入一次数据或建议任何其他方式来做到这一点。

4

1 回答 1

2

您已经定义了您的TimerFcn身份,{@open, hObject}但您没有名为 的函数openopen相反,它尝试使用三个输入参数(计时器对象、事件对象和)调用内置函数,hObject这会产生错误,因为open只接受一个输入参数。

话虽如此,根本不清楚您提供的代码将如何完成接近您想要的任何事情。像这样的东西可能会更好。

function samplegui_OpeningFcn(hObject, ~, handles, varargin)

    handles.timer = timer(...
            'ExecutionMode', 'fixedRate', ...   % Run timer repeatedly
            'Period', 300, ...                % Initial period.
            'TimerFcn', @(s,e)write_data()); % Specify callback

    handles.output = hObject;
    handles.vec_A=[];
    handles.vec_B=[];
    handles.vec_C=[];
    guidata(hObject, handles);

    start(handles.timer);

    %// Now update your data in a loop

    cnt = 0;

    while true
        %// Getting data  from Serial Port
        get_lines = fgets(handles.se) 
        if ~isempty(LINES)
            cnt = cnt + 1;   
            if strfind(LINES, 'T')
                handles.vec_A = [handles.vec_A; [timet newword]];
                plot(handles.vec_A(:,1), handles.vec_A(:,2:end),'r');
                drawnow
            end
        end
    end

    function write_data()
        %// Write it to file
        dlmwrite('My_sample1.txt',handles.vec_A);
        dlmwrite('My_sample2.txt',handles.vec_B);
        dlmwrite('My_sample3.txt',handles.vec_C);
    end
end
于 2016-04-18T13:15:09.143 回答