How can I call a function in a timer which was generated by the ImportData Tool?

1 vue (au cours des 30 derniers jours)
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!

Réponse acceptée

Walter Roberson
Walter Roberson le 23 Nov 2016
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'));
  1 commentaire
Roger Köhler
Roger Köhler le 23 Nov 2016
Thanks! Now I don't receive an error anymore - but now is the question: How do i get the Variables A,B,C,D,E from the importfile function in the workspace? If I am running the Timer it just shows me the Timer Object in the workspace.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by