importing a csv file into matlab automatically after it gets changed
9 ビュー (過去 30 日間)
古いコメントを表示
I have .csv file on my desktop which get replaced. I want to use a command in matlab which imports this file every time it gets changed into matlab for calculations. I got this formula:
fsw = System.IO.FileSystemWatcher();
fsw.Path = 'C:\Users\wolfx\Desktop';
fsw.Filter = 'filename.csv';
fsw.EnableRaisingEvents = true;
listenerhandle = addlistener(fsw, 'Changed', importfcn);
%signature of importfcn is function importfcn(sender, eventargs)
%add a small delay in importfcn before reading the file as the event is raised
%to make sure that file modification is complete
This formula gives me the error Undefined function or variable 'importfcn'.
Can anyone give me a hint or a site where I can read more about this?
thanks
4 件のコメント
Geoff Hayes
2014 年 12 月 7 日
Isn't importfcn a function that you have created? In a previous post of yours, http://www.mathworks.com/matlabcentral/answers/163047-automatic-import-into-matlab-after-time-period, Guillame's answer included the following code
t = timer;
t.Period = 49 * 60;
t.TimerFcn = importfcn; %for you to define with signature: function
% importfcn(obj, event)
Note his comment - you have to define the function with the specified signature. Since you accepted his answer, then you must have created this file and so you must know which folder has it. So once you have found the file, you can add its folder to the search path in any number of ways, including using addpath.
採用された回答
matt dash
2014 年 12 月 9 日
The errror is because when you just type "importfcn" it is attempting to RUN importfcn. What you're trying to do is tell the addlistener function "the NAME of the function i want you to run is "importfcn". You do this with the @ symbol. Thus you want:
listenerhandle = addlistener(fsw, 'Changed', @importfcn);
3 件のコメント
matt dash
2014 年 12 月 9 日
Ah, yes, that would be the "do something with the data" comment in Geoff's code above. If you're trying to send the data back to the base matlab workspace, look at the "assignin" function, which you can use in the importfcn variable to define a variable in the base workspace. E.g.
assignin('base','kevin',kevin)
その他の回答 (1 件)
Geoff Hayes
2014 年 12 月 7 日
AA - try creating a function as follows (saving this to file as importfcn.m or as a nested function within your other code)
function importfcn(obj, event)
% wait an extra second to ensure that the file modification is complete
pause(1.0);
% read the data from file
fileToRead = fullfile('C:\Users\wolfx\Desktop','filename.csv');
data = csvread(fileToRead);
% do something with the data
If you nest the function, then you can make use of fsw.Path and fsw.Filter in building the fileToRead and not have to hard-code the path and file name.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Import and Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!