在MATLAB中从一个类的一个函数中调用变量到另一个函数中

huangapple go评论96阅读模式
英文:

Calling variables from one function to another in a class to in MATLAB

问题

我有主脚本文件和一个类文件。在类文件中,我有两个函数(funkfunk1),在这些函数中,我有一些变量,我从主脚本中调用这些变量。

然而,如果我在类的一个函数中有一个变量,如何在类的另一个函数中使用相同的变量(可以是输入或输出)?以下是一个示例。

  1. classdef ga_clas
  2. % 电池约束
  3. properties
  4. %Property1
  5. end
  6. methods (Static)
  7. function[a,b,c,d]=funk(f,g,h,i,j,k,l)
  8. % 所有输入的值都来自主脚本
  9. for j=1:24
  10. g(j)=f(j)+k(j)
  11. end
  12. % g 是类中的变量,可以在另一个函数中用作输出,我不确定我是否使用正确。
  13. end
  14. function [g, M, N]=funk1(t,y,u,i,f)
  15. % 如果我需要使用前一个函数(funk1)中的变量,它可以是输入或输出,那么我可以在这里使用吗?
  16. end
  17. end
  18. end
英文:

I have the main script file and a class file. In the class file, I have two functions (funk and funk1) and in the functions, I have several variables which I call from the main script.

However, if I have a variable in one function of the class, how can I use the same variable in another function of the class (it could be both as input or output)? Below is an example.

  1. classdef ga_clas
  2. % The battery constraints
  3. properties
  4. %Property1
  5. end
  6. methods (Static)
  7. function[a,b,c,d]=funk(f,g,h,i,j,k,l)
  8. % The value of all input are from main script
  9. for j=1:24
  10. g(j)=f(j)+k(j)
  11. end
  12. % g is the variable in the class that can be used as output in another function, I'm not sure whether I'm using it correctly or not.
  13. end
  14. function [g, M, N]=funk1(t,y,u,i,f)
  15. % and If I have to use variables from the previous function (funk1) which could be input or output then can I use it here?
  16. end
  17. end
  18. end

答案1

得分: 2

1: 正如Cris Luengo提到的,你可以返回g。代码如下所示:

  1. function [a, b, c, d, g] = funk(f, g, h, i, j, k, l)
  2. for j = 1:24
  3. g(j) = f(j) + k(j);
  4. end
  5. end

然后,当你调用funk函数时,可以这样做:

  1. main
  2. %其他代码
  3. [a, b, c, d, g] = ga_class(f, g, h, i, j, k, l)

2: 将g设置为ga_class的属性:

  1. properties
  2. g = [] %我不知道g的类型,可以赋值为0或其它类型
  3. end
  4. methods (Static)
  5. function [a, b, c, d] = funk(self, f, g, h, i, j, k, l)
  6. %传入的self将是ga_class的实例
  7. for j = 1:24
  8. g(j) = f(j) + k(j);
  9. end
  10. self.g = g; %这将把它分配给这个方法的属性
  11. end

然后,稍后你可以这样调用:

  1. ga_class.g

你很可能想要选择第一种方式,但第二种方式也是可行的。

英文:

There's two ways of doing this.

1: as Cris Luengo mentioned, you can return g. This would look something like this:

  1. function[a,b,c,d,g]=funk(f,g,h,i,j,k,l)
  2. for j=1:24
  3. g(j)=f(j)+k(j)
  4. end
  5. end

Then, when you call funk you would do:

  1. main
  2. %other code
  3. [a, b, c, d, g] = ga_class(f, g, h, i, j, k, l)

2: set g as a property of ga_class:

  1. properties
  2. g = [] %i don't know what type g is could be assigned as 0 or whatever it's type is
  3. end
  4. methods (Static)
  5. function[a,b,c,d]=funk(self, f,g,h,i,j,k,l)
  6. %self being passed in would be an instance of ga_class
  7. for j=1:24
  8. g(j)=f(j)+k(j)
  9. end
  10. self.g = g %this assigns it into this method's properties
  11. end

Then, later you can call:
ga_class.g

You most likely want the first way, but it is possible to do it the second way.

huangapple
  • 本文由 发表于 2023年7月10日 20:44:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定