I'd like to understand how Matlab works with array objecs. I've read several posts and the Matlab help for this topic, but I still don't understand it completely.
Let's take a use case: I'd like to manage several measurement channels (the amount of channels can vary). Each measurement channel is an object with several properties. Now I'd like to have a class handling the channels (channelHandler.m). In this class I can simply add a new channel to the array (later on there might be bore functionality).
So what I've tried so far:
1) create the measurementChannel.m class
In the constructor I've only set the channel name so far without data.
classdef measurementChannel
%CHANNEL holds an instance of a single channel
properties
channelData
channelName = strings
channelUnit = strings
channelDataLength
channelOriginMeasurementFile
end
methods
function obj = channelTest(channelName)
if nargin > 0
obj.channelName = channelName;
end
end
end
end
To test this class I tried this:
channel(1) = measurementChannel('channelA');
channel(2) = measurementChannel('channelB');
channel(1).channelName
channel(2).channelName
which was working well.
2) Now I've created the channelHandler class:
classdef channelHandler
properties (Access = public)
channelArray
end
methods (Access = public)
function addChannel(obj, Name)
testobj = measurementChannel();
testobj.channelName = Name;
obj.channelArray = [obj.channelArray testobj];
end
end
and access this by using the following commands:
createChannels = channelHandler();
createChannels.addChannel('channel1');
createChannels.addChannel('channel2');
createChannels.channelArray(1).channelName
createChannels.channelArray(2).channelName
this fails because channelArray is not defined as an array and will give an error accessing channelArray(2).
So I also tried to initialize the array (but then I need to know the amount of channels).
so my questions are:
a) do I really need to initialize an array of objects?
b) how can I fix the channelHandler class to add objects to the array?
The issue is that you are not inheriting from the handle class and therefore the modifications made within addChannel alter a copy of your object rather than the object itself. If you inherit from handle, the code that you have pasted will work just fine.
classdef channelHandler < handle
Related
I am creating an app that has to create controls with specific parameters (some of which are custom parameters used for other applications) so the original method was like this
PSEDO CODE(kinda)
Dim ControlType1Color() as color
Dim ControlType1Name() as string
Dim ControlType1Parameter() as string
...
Dim ControlType2Color() as color
...
However when implementing that notation it ends up causing me to do quite a few test statements in order to recognize which type of control it is, Then find the correct variable to use. Which is honestly a waste of code space which could be saved.
While I was researching for something similar to java Objects (i believe?) I came across collections which should be what I need. However I need to know how I can implement it for this? Does a collection have infinite length? Can you use it as an array? And can you nest it with an array so you have an Array of Collections or is that just unnecessary?
EDIT FOR CLARIFICATION:
I am trying to record my controls that I created in run-time basically and try to use a single variable to record all Data
Lets say I have two textboxes and one button
My "Array of Collections" or whatever it would be would be like
AoC(0).Color = color
AoC(0).Type = Textbox
AoC(0).ID = ID
Aoc(1).Type = Textbox
...
Aoc(2).Type = Button
...
So if I needed to change anything I just change this thing only. I can handle the actual changes in code, I just need to know how to store it in the memory without using 5-15 variables needlessly.
You need to do something like this:
Private Structure ControlDetail
Public Color As System.Drawing.Color
Public ControlType As Type
Public ID As String
End Structure
Then you can define your data as:
Dim Aoc = New ControlDetail() _
{ _
New ControlDetail() With { .Color = System.Drawing.Color.Red, .ControlType = GetType(TextBox), .ID = "txtFoo1" }, _
New ControlDetail() With { .Color = System.Drawing.Color.Blue, .ControlType = GetType(Button), .ID = "butFoo2" } _
}
Then you can get access to the data as Aoc(0).ID, for example.
I am very new to VBScript, and not entirely sure if what I am doing is right.
I want to create a structure to hold onto a string and then an array of strings. The array of string will be dynamic, as I do not know how many entries are going to be in that list.
I have the following:
Class ExportMappings
Private _process_definition
Private _export_mappings : Set _export_mappings = CreateObject("System.Collection.ArrayList")
Public Property Let ProcessDefinition(procDef)
_process_definition= procDef
End Property
Public Property Get ProcessDefinition()
ProcessDefinition = _process_definition
End Property
Public Property Let ExportMappings(export)
_export_mappings = export
End Property
Public Sub AddMapping(map)
_export_mappings.Add map
End Sub
End Class
First, I am not sure if I declared the _export_mapping array properly.
Secondly, I do not know if I need a constructor to initialize my _export_mappings to an initial size. If so, I do not know how I would do that.
Lastly, my get and set methods for ExportMapping, I am not sure if that will work.
I would try to run it through a debugger, but the software I am using does not have the best debugger, and usually gives me a very vague description of what is wrong.
First things first:
VBScript variable names can't start with _; you can 'legalize' invalid names by putting them in [], but for starters I wouldn't do this
Code in Classes is allowed in methods only; your Private _export_mappings : Set _export_mappings = CreateObject("System.Collection.ArrayList") is invalid
After these changes, your code should compile. If you add code showing how you'd like to use this class, I'm willing to talk about second things (maybe tomorrow).
I have recently started evaluating Dapper as a potential replacement for EF, since I was not too pleased with the SQL that was being generated and wanted more control over it. I have a question regarding mapping a complex object in my domain model. Let's say I have an object called Provider, Provider can contain several properties of type IEnumerable that should only be accessed by going through the parent provider object (i.e. aggregate root). I have seen similar posts that have explained using the QueryMultiple and a Map extension method but was wondering how if I wanted to write a method that would bring back the entire object graph eager loaded, if Dapper would be able to do this in one fell swoop or if it needed to be done piece-meal. As an example lets say that my object looked something like the following:
public AggregateRoot
{
public int Id {get;set;}
...//simple properties
public IEnumerable<Foo> Foos
public IEnumerable<Bar> Bars
public IEnumerable<FooBar> FooBars
public SomeOtherEntity Entity
...
}
Is there a straightforward way of populating the entire object graph using Dapper?
I have a similar situation. I made my sql return flat, so that all the sub objects come back. Then I use the Query<> to map the full set. I'm not sure how big your sets are.
So something like this:
var cnn = sqlconnection();
var results = cnn.Query<AggregateRoot,Foo,Bars,FooBar,someOtherEntity,AggregateRoot>("sqlsomething"
(ar,f,b,fb,soe)=>{
ar.Foo = f;
ar.Bars = b;
ar.FooBar = fb;
ar.someotherentity = soe;
return ar;
},.....,spliton:"").FirstOrDefault();
So the last object in the Query tag is the return object. For the SplitOn, you have to think of the return as a flat array that the mapping will run though. You would pick the first return value for each new object so that the new mapping would start there.
example:
select ID,fooid, foo1,foo2,BarName,barsomething,foobarid foobaritem1,foobaritem2 from blah
The spliton would be "ID,fooid,BarName,foobarid". As it ran over the return set, it will map the properties that it can find in each object.
I hope that this helps, and that your return set is not too big to return flat.
I would like to create a new array with a given type from a class object in GWT.
What I mean is I would like to emulate the functionality of
java.lang.reflect.Array.newInstance(Class<?> componentClass, int size)
The reason I need this to occur is that I have a library which occasionally needs to do the following:
Class<?> cls = array.getClass();
Class<?> cmp = cls.getComponentType();
This works if I pass it an array class normally, but I can't dynamically create a new array from some arbitrary component type.
I am well aware of GWT's lack of reflection; I understand this. However, this seems feasible even given GWT's limited reflection. The reason I believe this is that in the implementation, there exists an inaccessible static method for creating a class object for an array.
Similarly, I understand the array methods to just be type-safe wrappers around JavaScript arrays, and so should be easily hackable, even if JSNI is required.
In reality, the more important thing would be getting the class object, I can work around not being able to make new arrays.
If you are cool with creating a seed array of the correct type, you can use jsni along with some knowledge of super-super-source to create arrays WITHOUT copying through ArrayList (I avoid java.util overhead like the plague):
public static native <T> T[] newArray(T[] seed, int length)
/*-{
return #com.google.gwt.lang.Array::createFrom([Ljava/lang/Object;I)(seed, length);
}-*/;
Where seed is a zero-length array of the correct type you want, and length is the length you want (although, in production mode, arrays don't really have upper bounds, it makes the [].length field work correctly).
The com.google.gwt.lang package is a set of core utilities used in the compiler for base emulation, and can be found in gwt-dev!com/google/gwt/dev/jjs/intrinsic/com/google/gwt/lang.
You can only use these classes through jsni calls, and only in production gwt code (use if GWT.isProdMode()). In general, if you only access the com.google.gwt.lang classes in super-source code, you are guaranteed to never leak references to classes that only exist in compiled javascript.
if (GWT.isProdMode()){
return newArray(seed, length);
}else{
return Array.newInstance(seed.getComponentType(), length);
}
Note, you'll probably need to super-source the java.lang.reflect.Array class to avoid gwt compiler error, which suggests you'll want to put your native helper method there. However, I can't help you more than this, as it would overstep the bounds of my work contract.
The way that I did a similar thing was to pass an empty, 0 length array to the constructor of the object that will want to create the array from.
public class Foo extends Bar<Baz> {
public Foo()
{
super(new Baz[0]);
}
...
}
Baz:
public abstract class Baz<T>
{
private T[] emptyArray;
public Baz(T[] emptyArray)
{
this.emptyArray = emptyArray;
}
...
}
In this case the Bar class can't directly create new T[10], but we can do this:
ArrayList<T> al = new ArrayList<T>();
// add the items you want etc
T[] theArray = al.toArray(emptyArray);
And you get your array in a typesafe way (otherwise in your call super(new Baz[0]); will cause a compiler error).
I had to do something similar, I found it was possible using the Guava library's ObjectArrays class. Instead of the class object it requires a reference to an existing array.
T[] newArray = ObjectArrays.newArray(oldArray, oldArray.length);
For implementing an array concatenation method, I also stepped into the issue of missing Array.newInstance-method.
It's still not implemented, but if you have an existing array you can use
Arrays.copyOf(T[] original, int newLength)
instead.
Following grails domain class:
class MyClass {
Map myMap
}
Now for myMap, grails automatically creates a new table for the elements in the map. However if I add elements which are too long (e.g. 1024 characters), I get a DB error.
Can I somehow tell grails to make the respective column in myMap's table big enough to allow for larger Strings, or do I have to do this manually in the DB?
I already tried
static constraints = {
myMap(maxSize:1024)
}
which doesn't work (as expected because maxSize should refer to the Map's values and not to the Map itself).
If not via constraints, maybe there's a way to do it via
static mapping { ... }
?
An alternative approach I used successfully was to push the map out into a collection of a collaborator domain class.
class DynaProperty {
String name
String value
static belongsTo = MyClass
static constraints = {
value(maxSize:4000) //Or whatever number is appropriate
}
}
And then in MyClass:
class MyClass {
static hasMany = [dynaProperties:DynaProperty]
}
This is almost a map, and it gives you the ability to use dynamic finders to pull up an individual entry.
what are you trying to accomplish? Is there always the same number of things in the map? If there is you should define those properties on your class.
You can see the problem with your current approach -- there is no way to figure out what might be in the map until runtime, so how can grails possibly create a columns for it? Im surprised it even worked to begin with...