Using Multiple Classes/Files MATLAB

81 次查看(过去 30 天)
Hello,
I am having some trouble understanding object oriented progrraming in MATLAB.
I have created a script .m file which I want my general code to run in and produce the output(s) that I want.
I have also create three separate .m files, each of which contain three different classes, and these classes each have a constructor as well as a function which returns a public variable of the class which is initialized thru the constructor.
My goal is to create an instance of these three different classes in the script .m file as I earlier mentioned, as I want to access this public variable from each class in the main script file as it is needed to run my program/produce the output.
For reference, here is an example of the class I am talking about:
classdef className
properties
%general variables
%variable i want accessed the main script
end
methods
function r = className(%general variables)
%variable i want accessed=(math operation using general
%variables)
r=className
end
function randomName = randomName()
randomName=%variable i want accessed int he main script
end
end
end

采纳的回答

recent works
recent works 2023-8-19
Create Class Files: You mentioned that you have three separate .m files, each containing a class. Let's assume these class files are named Class1.m, Class2.m, and Class3.m. Inside each of these files, define your classes similar to what you've shown in your example.
Class Definitions: Your class definitions in Class1.m, Class2.m, and Class3.m should look something like this:
classdef Class1
properties
% Define properties
Prop1
end
methods
function obj = Class1(arg1, arg2)
% Constructor
% Perform calculations using arg1 and arg2
obj.Prop1 = % calculated value;
end
function result = SomeMethod(obj)
% Method
result = % some calculation using obj.Prop1;
end
end
end
Repeat this structure for Class2.m and Class3.m, adjusting property names, constructor calculations, and methods as needed.
Main Script File: Now, in your main script file (let's call it mainScript.m), you can create instances of these classes and call their methods. Here's an example:
% Create instances of the classes using the constructors
obj1 = Class1(arg1_value, arg2_value);
obj2 = Class2();
obj3 = Class3();
% Call methods and access properties
result1 = obj1.SomeMethod();
result2 = obj2.SomeMethod();
result3 = obj3.SomeMethod();
% Access properties
prop1_value = obj1.Prop1;
Replace arg1_value and arg2_value with the actual values you want to pass to the constructors of Class1.
In your example, it seems like the classes are missing some details, but the general structure should be similar to what I've outlined. Here's a breakdown of what's happening:
You define classes in separate .m files.
Each class has properties (variables) and methods (functions).
The constructor (function obj = Class1(arg1, arg2)) initializes the properties based on the arguments you provide when creating an instance.
Methods can access the properties and perform calculations using them.
In the main script, you create instances of the classes and use their methods and properties.
  8 个评论
Steven Lord
Steven Lord 2024-4-29
I assume you mean my_uint8 and my_uint16, not the types included in MATLAB. You cannot change how uint8 and uint16 are defined or have them list inferior classes.
Thomas Bewley
Thomas Bewley 2024-5-1
编辑:Thomas Bewley 2024-5-1
of course. And it is done. :) FYI, I implemented a possibly interesting behavior, that automatically extends the data type to the next class up when it is necessary to prevent overflow, as illustrated in the snippet below. Perhaps the most useful part is that it throws an error, instead of returning a wrong result, when overthrow happens (unlike Matlab builtin). My class definitions that result in this behavior are available for free (under BSD 3-clause) at the link below (feel free to clone the entire RR repo, I'm collecting some hopefully useful stuff there). :)
>> A=RR_int8(1); for i=2:21, A=A*i, end
A =
RR_int8 with value 0x02 = 2
A =
RR_int8 with value 0x06 = 6
A =
RR_int8 with value 0x18 = 24
A =
RR_int8 with value 0x78 = 120
Warning: product overflow in RR_int8, increasing int type to RR_int16.
A =
RR_int16 with value 0x02D0 = 720
A =
RR_int16 with value 0x13B0 = 5040
Warning: product overflow in RR_int16, increasing int type to RR_int32.
A =
RR_int32 with value 0x00009D80 = 40320
A =
RR_int32 with value 0x00058980 = 362880
A =
RR_int32 with value 0x00375F00 = 3628800
A =
RR_int32 with value 0x02611500 = 39916800
A =
RR_int32 with value 0x1C8CFC00 = 479001600
Warning: product overflow in RR_int32, increasing int type to RR_int64.
A =
RR_int64 with value 0x000000017328CC00 = 6227020800
A =
RR_int64 with value 0x000000144C3B2800 = 87178291200
A =
RR_int64 with value 0x0000013077775800 = 1307674368000
A =
RR_int64 with value 0x0000130777758000 = 20922789888000
A =
RR_int64 with value 0x0001437EEECD8000 = 355687428096000
A =
RR_int64 with value 0x0016BEECCA730000 = 6402373705728000
A =
RR_int64 with value 0x01B02B9306890000 = 121645100408832000
A =
RR_int64 with value 0x21C3677C82B40000 = 2432902008176640000
Error using *
product overflow in RR_int64.
>>
I got started on all this because I needed standard integer division [Q,R]=A/B defined, where (as opposed to / in Matlab),
A = (A/B)*B + R, where sign(R)=sign(A) and |R|<|B|, where A=dividend, B=divisor, thus satisfying the following standard identities: (−A)/B = −(A/B) = A/(−B).
This is now implemented in my family of int, classes which includes:
RR_int8, RR_int16, RR_int32, RR_int64, and
RR_uint8, RR_uint16, RR_uint32, RR_uint64, RR_uint128, RR_uint256, RR_uint512, RR_uint1024
Using this (and my random number generation routines) the following calculations (at all levels of precision implemented) give C=0, |R|<|B|, sign(R)=sign(A), and Q1=Q2=Q3 in the following types of tests:
>> A=RR_rand_RR_int([-10^16,10^16]), B=RR_rand_RR_int([-10^10,10^10])
if B~=0, [Q,R]=A/B, C=(Q*B+R)-A, Q1=(-A)/B, Q2=-(A/B), Q3=A/(-B), end
A =
RR_int64 with value 0xFFFF49BC1F043B62 = -200402653660318
B =
RR_int64 with value 0x000000024CF63C68 = 9881140328
Q =
RR_int64 with value 0xFFFFFFFFFFFFB0C7 = -20281
R =
RR_int64 with value 0xFFFFFFFF3E7BC68A = -3246668150
C =
RR_int64 with value 0x0000000000000000 = 0
Q1 =
RR_int64 with value 0x0000000000004F39 = 20281
Q2 =
RR_int64 with value 0x0000000000004F39 = 20281
Q3 =
RR_int64 with value 0x0000000000004F39 = 20281
>>

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by