現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
how to generate a string from the function content besides running it
5 ビュー (過去 30 日間)
古いコメントを表示
S H
2019 年 5 月 27 日
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?
S H
2019 年 5 月 27 日
編集済み: S H
2019 年 5 月 27 日
The script that has these generated functions is not a structure. It is a mix and match of different expressions followed by functions. I do not know how to access some of those functions (or call them) from other scripts. It would be very helpful if there is a way to call functions a, b and c in script #1 from script #2 without creating standalone functions a, b and c in separate files.
In my case, a gui generates script #1 that has many functions a,b,c,... that change for different systems being analyzed and it will be very tedious if for every system, I copy and pase each function in separate files and then call them by solver.
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.
S H
2019 年 5 月 27 日
編集済み: S H
2019 年 5 月 27 日
Is it possible to add the following comments to the functions in scripts #1 and then read their contents and add them into scripts #2?
.
.
.
function c=test(a)
%function contents start here
b=a.'+2;
%disp('linear mode')
c=3*b-1;
%function content ends here
end
.
.
.
The reader will scan script #1 and sarts generating strings of lines if
%function contents start here
is detected until
%function content ends here
and then repeats for the next match.
S H
2019 年 5 月 27 日
編集済み: S H
2019 年 5 月 27 日
I call an m file "function" if it only has fnctions.
Example for test.m:
function c=test(a)
b=a.'+2;
%disp('linear mode')
c=3*b-1;
end
I call an m file "script" if it has commands and functions.
Example for test1.m
disp('hello')
z=test(10)+5;
disp('end')
function c=test(a)
b=a.'+2;
%disp('linear mode')
c=3*b-1;
end
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 日
The benefit is I do not end up seeing hundreds of m files (containing standalone functions) in my drive. I write functions (existing in file #1) into one script (file #2) where they are needed.
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 日
Yes I do not know from which Matlab version it started but you can include functions at the end of an m file and it will work.
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 日
Geoff, is it okay to define the class at the end of an script where functions start to be defined such as the following m file
%start m file by putting some commands here
disp('hello')
tic
pause(1)
toc
%the following will be at the end of the m file
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
and then read test and test2 the way you mentioned earlier?
S H
2019 年 5 月 27 日
I just tested it and it is not possible to have anything above classdef MyClass line
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 日
Thank you Geoff and Walter for introducing class. I will read about it and see how I can modify my gui output to benefit from class offerings.
回答 (0 件)
参考
カテゴリ
Help Center および 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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
