필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

translating a script into a function and executing it in the base workspace

조회 수: 1 (최근 30일)
AA
AA 2015년 1월 22일
마감: MATLAB Answer Bot 2021년 8월 20일
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
per isakson 2015년 1월 23일
편집: per isakson 2015년 1월 23일
  • "the above script to be written as a function" &nbsp The code above is already a function. See Scripts vs. Functions
  • "Why do I get that error?" &nbsp because "'fsw.Filter'" is not a legal name of a variable

답변 (3개)

Image Analyst
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
Image Analyst 2015년 1월 23일
There's no reason to send your functions variables directly to the base workspace. See the FAQ
If you want them there then the functions should just return them and you call the function from a simple script.
AA
AA 2015년 1월 23일
How can I incorporate a code in the function so that the external script gets executed and the variables get enlisted in the base workspace? If I achieve this then I do not need to put the script in the function.

Star Strider
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.

steven7337
steven7337 2015년 1월 22일
Just type the name of the script on command line, then it will be executed.
  댓글 수: 1
AA
AA 2015년 1월 22일
I cant do that. I want the script to be incorporated as a function into another function and then display the variables in the base workspace. It is all part of a great plan to accomplish my algorithm.

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by