How the struct definition works?

2 次查看(过去 30 天)
I am trying to understand the code written by someone in matlab and I am attempting to understand and to translate it to fortran. The sample code I am reading is shown below . if bucket1.bucket1 is not equal to 'y' then its structure will be character, integer as defined before. However, if its equal to y then bucket1.bucket1 will equal to 5,1 which is integer, integer. When compiling in matlab there is no error and his code works. However when writting in fortran, there is structure definition collision. How come matlab is not complaining when character is replaced with a number? Bucket1 is defined as a list of characters and bucket2 is defined as list of integers.
samples = struct(...
'bucket1' {'x','y','z'}...
'bucket2' {1,2,3})
if bucket1.bucket1 == 'y'
bucket1.bucket1 = 5
end
disp(bucket1.bucket1)
  1 个评论
Stephen23
Stephen23 2019-1-28
编辑:Stephen23 2019-1-28
The code refers to a variable named bucket1, but nowhere is such a variable defined. The code only define a structure named samples, which is never referred to again after being defined.
"if bucket1.bucket1 is not equal to 'y' then..."
bucket1.bucket1 does not exist in the code you have shown us, and trying to refer to a non-existant variable will throw an error. Also the vague "if its equal to y" does not reflect that eq actually performs element-wise comparison of all characters, so it would be better described as "if all characters are 'y' ... ".
"Bucket1 is defined as a list of characters and bucket2 is defined as list of integers."
MATLAB does not have a "list" class. Basic MATLAB data classes are described in the MATLAB documentation:
"How the struct definition works?"
Read the struct help: it explains that that struct call will create a 1x3 struct with two fields, which in this case initially contain scalar character arrays and scalar numerics. The size of the cell arrays determines the structure size. Nothing in that structure resembles a "list" (i.e. character arrays and numeric arrays are not container variables, as "lists" are in many programming languages).
"How come matlab is not complaining when character is replaced with a number?"
Simple: every field's content is totally independent from all other structure elements and all other fields. There is absolutely no requirement in MATLAB for the same field of different structure elements to have the same data class.

请先登录,再进行评论。

采纳的回答

mathango
mathango 2019-1-28
So what is the equivalent in Fortran language, to dynamically type the gene variable?
  2 个评论
Image Analyst
Image Analyst 2019-1-28
I don't know.
You should ask Fortran questions in a Fortran forum.
Walter Roberson
Walter Roberson 2019-1-28
you can %ptr and once you have figured out the needed type pass the %ptr to a conversion routine .

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2019-1-28
The code will not run unless bucket1 has been declared/instantiated somewhere else.
A structure "samples" was created, but never used in the code that you posted.
Assuming bucket1 was also created in the same way that samples was, it would (assuming several syntax errors are corrected) check to see if bucket1.bucket1 was the character y, and if so, it would change the type of the variable to a double 5. This is not, in my opinion, a good idea (to change the class of a variable from one type to another).
  4 个评论
Walter Roberson
Walter Roberson 2019-1-28
MATLAB is dynamically typed. It is completely valid in MATLAB to have
AA = true;
AA = 5;
AA = 'hello';
AA = figure();
in a single function. It might not result in the clearest of code, but it is completely valid and will not result in any error.
In the case of "complete" variables such as I have given here, AA, not indexed or structure referenced, it is not possible to prevent a completely different data type from overwriting the variable.
In cases where there is some kind of indexing, such as
AA.param1 = 'hello';
AA.param1 = 5;
then there are ways in MATLAB where the change of data type could potentially be caught, but they would have to be specifically programmed.
There are cases, such as calling into an external library, where it can be appropriate that the same name might refer to different data types at different times. For example,
inputs.functname = 'add';
inputs.numparam = 2;
inputs.param1 = 3;
inputs.param2 = 5;
result = call_library('my_external_library', inputs);
inputs.functname = 'display';
inputs.numparam = 2;
inputs.param1 = '%d';
inputs.param2 = result;
result = call_library('my_external_library', inputs);
Walter Roberson
Walter Roberson 2019-1-28
MATLAB does not have the equivalent of fortran COMMON declaring a block of memory with mixed data to be potentially different sizes and types.
MATLAB does have the ability to reinterpret arrays of basic numeric datatypes as if they were other data types. For example,
>> typecast(uint8([1 2 3 4]),'uint16')
ans =
1×2 uint16 row vector
513 1027
However this is limited. You cannot, for example,
typecast('hello', 'uint8')
whereas in fortran you could have two different COMMON declarations with the same name but one declaring character and one declaring numeric. The size of characters is not completely defined in MATLAB... but the definitions that are present are as-if characters are 16 bits in MATLAB.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Fortran with MATLAB 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by