我试图使用MATLAB上的计时器在最后一次仅将数据写入excel。(MATLAB版本R2017a)
我想做两件事: 1)当我执行下面参考示例代码编写的程序时,出现错误,并且不知道如何解决该错误。
错误消息
>> timer_sample
x_value =
1 2 3
y_value =
4 5 6
x_value =
1 2 3 7 8 9
y_value =
4 5 6 10 11 12
Error while evaluating TimerFcn for timer 'timer-1'
Error: Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
2)当前,不使用disp()函数显示x_value和y_value的值,但我想防止显示此值。
作为另一种方法,我已经执行了timer_sample.m,并且使用MATLAB上的面向对象编程方法将TimerCallback.m与timer_sample.m放在同一文件夹中。
但是,我收到以下错误消息。 我该如何解决?有什么问题?
Error Message
>> timer_sample
Attempt to execute SCRIPT varargin as a function:
/Applications/MATLAB_R2019a.app/toolbox/matlab/lang/varargin.m
Error in TimerCallback (line 11)
if varargin > 1
Error in timer_sample (line 1)
mycallback = TimerCallback('data.xlsx');
timer_sample m
mycallback = TimerCallback('data.xlsx');
mytimer = Timer('TimerFcn', @mycallback.callback, 'StartDelay', 30);
mycallback.x_value = [mycallback.x_value, [1 2 3]]
mycallback.y_value = [mycallback.y_value, [4 5 6]]
mycallback.x_value = [mycallback.x_value, [7 8 9]]
mycallback.y_value = [mycallback.y_value, [10 11 12]]
%and so on... until the Timer completes
TimerCallback.m
classdef TimerCallback < handle
properties %public properties
state;
x_value;
y_value;
filename;
end
methods
%constructor
function this = TimerCallback(filename)
if varargin > 1
this.filename = filename;
end
this.state = true;
end
%callback function
function callback(this, ~, ~)
%consider using writetable instead of xlswrite. At least, use only one xlswrite.
xlswrite(this.filename, this.x_value, 'sender');
xlswrite(this.filename, this.y_value, 'receiver');
this.state = false;
end
end
end
我执行的程序
global x_value
global y_value
time = timer('TimerFcn', 'stat=false; disp(''Timer!'') filename = ''data.xlsx''; x_range = ''sender''; y_range = ''receiver''; xlswrite(filename, x_value, x_range) xlswrite(filename, y_value, y_range) exit();', 'StartDelay', 30);
start(time)
%flag to write code after 30 seconds
global stat
stat = true;
x_value = [x_value [1 2 3]]
y_value = [y_value [4 5 6]]
x_value = [x_value [7 8 9]]
y_value = [y_value [10 11 12]]
我试图做到的 没有时间,我可以确保像下面这样提供excel输出。
global x_value
global y_value
global stat
stat = true;
x_value = [x_value [1 2 3]]
y_value = [y_value [4 5 6]]
x_value = [x_value [7 8 9]]
y_value = [y_value [10 11 12]]
filename = 'data.xlsx';
x_range = 'sender';
y_range = 'receiver';
xlswrite(filename, x_value, x_range)
xlswrite(filename, y_value, y_range)