Azzera filtri
Azzera filtri

importing a csv file into matlab automatically after it gets changed

16 visualizzazioni (ultimi 30 giorni)
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 Commenti
Geoff Hayes
Geoff Hayes il 7 Dic 2014
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.
AA
AA il 7 Dic 2014
I didnt create the function file importfcn. that is why i cannot find it. how can i create this function file? i have never done this before.

Accedi per commentare.

Risposta accettata

matt dash
matt dash il 9 Dic 2014
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 Commenti
matt dash
matt dash il 9 Dic 2014
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)
AA
AA il 10 Dic 2014
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\wolfgang\Desktop\NZDUSD1.csv');
circa = xlsread(fileToRead);
kevin=0.5*(circa(:,4)+circa(:,5)).'
assignin('base','kevin',kevin)
% do something with the data
end
fsw = System.IO.FileSystemWatcher();
fsw.Path = 'C:\Users\wolfgang\Desktop';
fsw.Filter = 'NZDUSD1.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

Accedi per commentare.

Più risposte (1)

Geoff Hayes
Geoff Hayes il 7 Dic 2014
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.

Categorie

Scopri di più su Data Import and Analysis in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by