I am trying to initialize a derived type using a parameter declaration. When I compile, I get the following error
Element in INTEGER(4) array constructor at (1) is CHARACTER(1).
User defined kinds values ip and dp are found in fasst_global. They are:
integer,parameter:: ip = selected_int_kind(8)
integer,parameter:: dp = selected_real_kind(15,307)
I have tried using 1_ip instead of 1 as the first element and it made no difference. What am I doing wrong?
module fasst_derived_types
use fasst_global
implicit none
type fasst_default_soil
integer(ip):: sid
character(len=2):: ssname
real(dp):: dens,pors,ssemis,ssalb,shc,smin,smax,salpha,svgn
real(dp):: sspheat,sorgan,spsand,spsilt,spclay,spgravel
end type fasst_default_soil
type(fasst_default_soil),parameter:: fasst_soil(1) = fasst_default_soil( &
(/1, 'GW',1.947_dp,0.293_dp, 0.92_dp,0.40_dp,1.1197e-2_dp, &
0.01_dp,0.293_dp,22.6125_dp, 3.45_dp, 820.0_dp, &
0.0_dp, 5.0_dp, 2.0_dp, 2.0_dp,91.0_dp/))
end module fasst_derived_types
You are trying to use two constructors here:
an array constructor;
a structure constructor.
You have the correct syntax for each, but you are using them incorrectly.
The array constructor (/.../) is to construct an array. But you want an array of derived type values (well, one value) rather than an array as the component for a single derived type value. The syntax error comes from attempting to create an array with various different/incompatible types.
So, instead you want
type(fasst_default_soil),parameter:: fasst_soil(1) = (/fasst_default_soil(1_ip,'GW', ...)/)
Or, as you just want a single element array you don't even need to construct that array of derived types.
Related
I have a class constructor that takes multiple numeric arrays of the same size and creates an object array of the same size. In the constructor, I am trying to use deal to assign data from each input array to a single index of the object array's only property:
classdef myClass
properties
data
end
methods
function obj = myClass(varargin)
nArg = length(varargin);
for iArg = 1:nArg
dataArg = num2cell(varargin{iArg});
[obj.data(iArg)] = deal(dataArg{:});
end
end
end
end
Unfortunately, I get the following error from deal: The number of outputs should match the number of inputs.
I believe that this is probably occurring because I haven't preallocated the object array before the for loop in the constructor, e.g.:
szObj = size(varargin{1});
cellSzObj = num2cell(szObj);
obj(cellSzObj{:}) = myClass.empty;
However, for various reasons, I want to be able to make this assignment without preallocating the object array.
Is there a way to do this?
I have below declaration:
Type routineStruct
man As String * 3
number As String * 5
End Type
Type vbToC
sch As String * 4
routine(0 To 10000) As routineStruct
End Type
vbfun As vbToC
Now we are sending this vbfun As vbToC from VB to C.
Now for above vbToC type , 64K memory limit error is occuring while compiling.
So, we use the dynamic array as like -
Type vbToC
sch As String * 4
routine() As routineStruct
End Type
And then assign value
Redim routine(10000)
For loop = 0 To 10000
.routine(loop).man = value1
.routine(loop).number = value2
Next
My question is above vbToc declaration for dynamic array is right or not? if right then can it be possible to send values from vb to c with this dynamic array and how to receive the value for dynamic array for each structure element?
In VB6 the type you have defined as "routineStruct" is invalid. You would most certainly receive a compile error here. If you are trying to declare "man" and "number" as arrays the syntax is not correct. You would need to use a declaration similar to your "routine" element with an array declaration.
Furthermore, you have declared your type without any access modifier. When you do this it will default to "private." If you were to export the VB6 ActiveX DLL to a type library for use in C++ the type would therefore not be visible and marshaling would not be possible.
The solution would be to adjust your type declaration to include proper VB6 syntax as well as make the type public. Marshaling of native string types become BSTR in C/C++ and you should have no problem here.
I can't seem to figure out how containers.Map works. It's doing okay with characters and numbers, but flips out when I try to feed it arrays. How do I make something like this?
function test
global a
a = containers.Map();
a(pi) = 3:14;
a(5) = 4:2:10;
end
The problem is that you're using the default constructor for the containers.Map class. From the help:
myMap = containers.Map() creates an empty object myMap that is an
instance of the MATLAB containers.Map class. The properties of myMap
are Count (set to 0), KeyType (set to 'char'), and ValueType (set to
'any').
In other words, you can only use strings as keys in this form. If you want to use arbitrary double precision values as keys, you need to specify the 'KeyType' and 'ValueType' in the constructor:
myMap = containers.Map('KeyType', kType, 'ValueType', vType) constructs
a Map object with no data that uses a key type of kType, and a value
type of vType. Valid values for kType are the strings: 'char',
'double', 'single', 'int32', 'uint32', 'int64', 'uint64'. Valid values
for vType are the strings: 'char', 'double', 'single', 'int8', 'uint8',
'int16', 'uint16', 'int32', 'uint32', 'int64', 'uint64', 'logical', or
'any'. The order of the key type and value type arguments is not
important, but both must be provided.
In your example:
a = containers.Map('KeyType','double','ValueType','any');
a(pi) = 3:14;
a(5) = 4:2:10;
Note, however, that a 'ValueType' of 'double' will not work for non-scalar values. Alternatively, you can specify your keys and values directly with cell arrays via the constructor and let it do the work of figuring out what types to use:
a = containers.Map({pi,5},{3:14 4:2:10});
There is no issue with putting arrays, cells, even other maps inside a map. The issue is your key field. As far as I know, Maps use strings as keys. So your current code won't work, but this would
function test
global a
a = containers.Map();
a('pi') = 3:14;
a('5') = 4:2:10;
end
I am using an array of objects in my program, and each object has several attributes. I want to be able to extract separate arrays from this array of objects, one array for each attribute.
here is a snippet of the relevant code:
dailyDataMatrix(m,n)=dailyData('',''); %creates an mxn array of dailyData objects
for i=1:m
for j=1:n
dailyDataMatrix(i,j)=dailyData(datainput1, datainput2)%dailyData constructor, sets attributes
end
end
dailyDataMatrix.attribute
But when I try to access a certain attribute as in the code above, say of type string, I get a strange result. Instead of getting an array of strings, I get something else. When I try to print it, rather than printing an array, it prints a series of individual values
ans = 'string1'
ans = 'string2'
...
When I try to call
size(dailyDataMatrix.attribute)
className = class(dailyDataMatrix.attribute)
I get
"error using size: too many input arguments" and
"error using class: The CLASS function must be called from a class constructor."
However, when I write this as
thing=dailyDataMatrix.attribute
className = class(thing)
size(thing)
I get the response
classname = 'double' and size = 1x1.
Why is this not returning an array the same size as dailyDataMatrix? And an aside question is why the two different ways of writing the code above give different results? and how can I get the result that I want?
Thanks,
Paul
You can capture all the outputs using a cell array or using square brackets if the types are same. For regular array when all values are of same type use,
thing=[dailyDataMatrix.attribute];
If the types are different you can use
thing = cell(1,N); % N is the number of elements in dailyDataMatrix
[thing{:}] = dailyDataMatrix.attribute;
ive set up a custom data type
type vector = {a:float;b:float};
and i want to Initialize an array of type vector but containing nothing, just an empty array of length x.
the following
let vecarr = Array.create !max_seq_length {a=0.0;b=0.0}
makes the array init to {a=0;b=0} , and leaving that as blank gives me errors. Is what im trying to do even possible?
You can not have an uninitialized array in OCaml. But look at it this way: you will never have a hard-to-reproduce bug in your program caused by uninitialized values.
If the values you eventually want to place in your array are not available yet, maybe you are creating the array too early? Consider using Array.init to create it at the exact moment the necessary inputs are available, without having to create it earlier and leaving it temporarily uninitialized.
The function Array.init takes in argument a function that it uses to compute the initial value of each cell.
How can you have nothing? When you retrieve an element of the newly-initialized array, you must get something, right? What do you expect to get?
If you want to be able to express the ability of a value to be either invalid or some value of some type, then you could use the option type, whose values are either None, or Some value:
let vecarr : vector option array = Array.create !max_seq_length None
match vecarr.(42) with
None -> doSomething
| Some x -> doSomethingElse
You can initialize and 'a array by using an empty array, i.e., [||]. Executing:
let a = [||];;
evaluates to:
val a : 'a array = [||]
which you can then append to. It has length 0, so you can't set anything, but for academic purposes, this can be helpful.