how to generate a string from the function content besides running it
이전 댓글 표시
Could you help me by adding some scripts to any function that will create a string from the function contents besides running the function?
For example, the following function will only generate 35 for test(10). I want it also to generate str as I have provided below.
.
.
.
function c=test(a)
b=a.'+2;
%disp('linear mode')
c=3*b-1;
end
.
.
.
The new function should be able to run as [out,str]=test(10) and provide the following result.
out =
35
str =
3×1 cell array
{["b=a.'+2;" ]}
{["%disp('saturation region')"]}
{["c=3*b-1;" ]}
댓글 수: 18
Geoff Hayes
2019년 5월 27일
To be clear, you want one of the output parameters from your function to be a cell array of each line of code in that function? Why?
Geoff Hayes
2019년 5월 27일
Can't you just call this function from the other scripts?
Walter Roberson
2019년 5월 27일
diary() perhaps
Geoff Hayes
2019년 5월 27일
편집: Geoff Hayes
2019년 5월 27일
The script that has these generated functions is not a structure.
Please clarify the above statement.
...call functions a, b and c in script #1 from script #2..
Where are these functions (a, b, and c) defined if not in separate files? I don't think that you can define functions inside a script. Unless your definition of a script differs from Scripts vs. Functions.
You may want to provide an example that clarifies your above statements.
Geoff Hayes
2019년 5월 27일
The reader will scan script #1...
And the "reader" is something that you have written and will know where to insert the code into the second script? I don't see how this is more beneficial than defining separate files for those functions that you would call from different scripts (or functions or classes etc.).
S H
2019년 5월 27일
Geoff Hayes
2019년 5월 27일
disp('hello')
z=test(10)+5;
disp('end')
function c=test(a)
b=a.'+2;
%disp('linear mode')
c=3*b-1;
end
And the above code is valid? i.e. you can save it to an m-file (called test1.m) and can run it without errors? I wasn't aware that you could include function definitions inside a script...my version doesn't support that...
S H
2019년 5월 27일
Geoff Hayes
2019년 5월 27일
편집: Geoff Hayes
2019년 5월 27일
If you are concerned about polluting your drive with hundreds of m-files, then consider creating a class that has several static methods/functions. You could group all related functions into a single class which your script or other functions would then call as needed. For example,
classdef MyClass
methods(Static)
function c = test(a)
b=a.'+2;
%disp('linear mode')
c=3*b-1;
end
function c = test2(a,b)
c = a * b;
end
end
end
is saved to a (single) file named MyClass.m. You would then call one of these methods as
c = MyClass.test(10);
or as
c = MyClass.test2(10,42);
S H
2019년 5월 27일
S H
2019년 5월 27일
Geoff Hayes
2019년 5월 27일
In my version (2014a) of MATLAB, a class needs to be defined in its own file. A problem with the above may be that the class isn't public and so wouldn't be visibie to any other script or function. (I say "may" because I'm not certain with your version....)
Walter Roberson
2019년 5월 27일
A class must be in its own file(s) .
scripts can include functions as of R2016b.
S H
2019년 5월 27일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!