How can I call a function in a timer which was generated by the ImportData Tool?
1 回表示 (過去 30 日間)
古いコメントを表示
Hello,
i created a timer function
function Timer
T= timer('ExecutionMode','fixedrate','TasksToExecute',10,'Period',10,...
'TimerFcn',{@importfile,'XYZ.csv'});
start(T)
and want to call a function importfile which was generated by the ImportData Tool in Matlab
function [A,B,C,D,E] = importfile(filename, startRow, endRow)
%IMPORTFILE Import numeric data from a text file as column vectors.
% [A,B,C,D,E] =
% IMPORTFILE(FILENAME) Reads data from text file FILENAME for the default
% selection.
%
% [A,B,C,D,E] =
% IMPORTFILE(FILENAME, STARTROW, ENDROW) Reads data from rows STARTROW
% through ENDROW of text file FILENAME.
%
% Example:
% [A,B,C,D,E] = importfile('XYZ.csv',1, 36);
%
% See also TEXTSCAN.
% Auto-generated by MATLAB on 2016/11/22 11:39:19
%%Initialize variables.
delimiter = ',';
if nargin<=2
startRow = 1;
endRow = inf;
end
%%Format string for each line of text:
% column1: text (%s)
% column2: text (%s)
% column3: double (%f)
% column4: text (%s)
% column5: double (%f)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%s%s%f%s%f%[^\n\r]';
%%Open the text file.
fileID = fopen(filename,'r');
%%Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'HeaderLines', startRow(1)-1, 'ReturnOnError', false);
for block=2:length(startRow)
frewind(fileID);
dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'HeaderLines', startRow(block)-1, 'ReturnOnError', false);
for col=1:length(dataArray)
dataArray{col} = [dataArray{col};dataArrayBlock{col}];
end
end
%%Close the text file.
fclose(fileID);
%%Post processing for unimportable data.
% No unimportable data rules were applied during the import, so no post
% processing code is included. To generate code which works for
% unimportable data, select unimportable cells in a file and regenerate the
% script.
%%Allocate imported array to column variable names
A = dataArray{:, 1};
B = dataArray{:, 2};
C = dataArray{:, 3};
D = dataArray{:, 4};
E = dataArray{:, 5};
When I run the Timer function I receive following error:
Error while evaluating TimerFcn for timer 'timer-30'
First input must be a file name of type char, or a file identifier of type double.
What am I doing wrong? I would also like to have the Variables A,B,C,D,E in the workspace after running the Timer function.
Thanks for your help!
0 件のコメント
採用された回答
Walter Roberson
2016 年 11 月 23 日
TimeFcn follows the standard of all callbacks: the first parameter passed to the callback is the object generating the callback, and the second parameter is a structure of data.
You should code
T= timer('ExecutionMode','fixedrate','TasksToExecute',10,'Period',10,...
'TimerFcn',{@(src, event) importfile('XYZ.csv'));
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!