- "the above script to be written as a function"   The code above is already a function. See Scripts vs. Functions
- "Why do I get that error?"   because "'fsw.Filter'" is not a legal name of a variable
Info
この質問は閉じられています。 編集または回答するには再度開いてください。
translating a script into a function and executing it in the base workspace
1 回表示 (過去 30 日間)
古いコメントを表示
function test()
fsw = System.IO.FileSystemWatcher();
fsw.Path = 'C:\Users\wolfgang';
fsw.Filter = 'test.csv';
fsw.EnableRaisingEvents = true;
listenerhandle = addlistener(fsw, 'Changed', @(~,~)testing1234(q,a));
%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
end
I need the above script to be written as a function so that it gets executed in the base workspace. How can I do that?
yI use assignin('base','fsw',fsw) and it gets to the base workspace. However, somehow i get an error when i do assignin('base','fsw.Filter',fsw.Filter). Why do I get that error?
1 件のコメント
per isakson
2015 年 1 月 23 日
編集済み: per isakson
2015 年 1 月 23 日
回答 (3 件)
Image Analyst
2015 年 1 月 22 日
Let's say it's called test.m. Simply put this line at the beginning of the file to turn it from a script into a function
function test()
5 件のコメント
Image Analyst
2015 年 1 月 23 日
If you want them there then the functions should just return them and you call the function from a simple script.
Star Strider
2015 年 1 月 22 日
編集済み: Star Strider
2015 年 1 月 23 日
At the risk of pointing out the obvious (and not having run your code), why not write it at the outset as:
function fsw = test()
and see if that does what you want.
EDIT —
It will return whatever variables you want it to return (here only ‘fsw’) to the workspace when the function completes and returns control to the calling script. See Create Functions in Files and related documentation for details.
0 件のコメント
steven7337
2015 年 1 月 22 日
Just type the name of the script on command line, then it will be executed.
1 件のコメント
この質問は閉じられています。
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!