How to label a multidimensional scaling plot when names are in struct - arrays

I made a multidimensional scaling plot with Matlab's mdscale function. According to the instructions
here, the points can be labelled with the gname function. However, the dummy data they use in the tutorial is structured in cells as far as I can see and I am using a struct array.
I have a 1x14 struct array with 2 fields: speaker and name, which I built like this:
speaker(1).data = importdata([path,'file1']);
speaker(1).name = 'speaker 1';
speaker(2).data = importdata([path,'file2']);
speaker(2).name = 'speaker 2';
Then I calculated a distance matrix "d" from speaker(i).data and performed multidimensional scaling:
[Y,stress] = mdscale(d,6,'criterion','metricstress');
plot(Y(:,1),Y(:,2),'o','LineWidth',2);
xlabel('Distance')
ylabel('Distance')
gname(speaker().name(distances))
The last line "gname..." is wrong, it gives me the error "Field reference for multiple structure elements that is followed by more reference blocks is an error."
I don't know how I can get the names displayed on my plot when I have a struct instead of a cell. Can somebody help please?
Edit:
I managed to convert speaker().name into a cell array "names" and call
gname(names)
It is working now! Thanks for the helpful comment.

Related

Error when trying to set array in userdefaults: Thread 1: "Attempt to insert non-property list object

I have solved the issue now, thanks for your help. I shouldn't have tried to save arrays with UITextViews, but I should have saved their text as strings instead. Here was the original question:
I have tried a lot, and googled a lot, but I can't solve this problem on my own. Whenever I try to save an array in userdefaults, it just is not working. I get the following error:
Thread 1: "Attempt to insert non-property list object (\n "<UITextView: 0x14001f800; frame = (0 0; 355 180); text = 'D'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600003f01d10>; layer = <CALayer: 0x6000031c83e0>; contentOffset: {0, 0}; contentSize: {355, 30}; adjustedContentInset: {0, 0, 0, 0}>"\n) for key content"
I don't know what a non-property list object is. And I do not know how to solve the problem. Below is the lines of code that do not work.
var contentList: [Any] = []
let cl = defaults.array(forKey: "content")!
if cl.count != 0{
contentList += cl
}
contentList.append(label)
defaults.setValue(contentList, forKey: "content")
If I take out the last line of code by turning it into a comment everything runs just fine. How should I replace that line of code? I essentially want to save an array of UITextViews and make it larger every time I call a fucntion (this code is part of a larger function). The reason why I have created another two lists (cl and contentList) is that it helps me with a problem down the line. What I cannot understand however, is why the last line of code doesn't work. If anyone has any ideas, please help me, it would be much appreciated.
Use only String as stated in comments :
var contentList: [String] = []
let cl = defaults.array(forKey: "content")!
if cl.count != 0{
contentList += cl
}
If lbText = label.text {
contentList.append(lbText)
defaults.setValue(contentList, forKey: "content")
}
You can only store a very limited list of data types into UserDefaults, commonly referred to as "property list objects" (Since property list (or plist) files will only store the same data types.
To quote the Xcode docs on UserDefaults, in the section titled "Storing Default Objects":
A default object must be a property list—that is, an instance of (or for collections, a combination of instances of) NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary [or Data, String, NSNumber, Date, Array, or Dictionary types in Swift.] If you want to store any other type of object, you should typically archive it to create an instance of Data.
(I added the equivalent Swift types to the above quote in square brackets, since it looks like Apple hasn't updated it for Swift.)
That's worded a little awkwardly. The idea is that you can only store data of the types listed. Because the Array and Dictionary types are "container" types, you can store any combination of arrays and dictionaries that contain combinations of any of the above types. For example, you can store an array that contains a dictionary, 3 dates, 2 floats, a Double, some Data, and 2 arrays, and those dictionaries and arrays can contain other dictionaries and/or arrays.)
It is almost always wrong to archive UIView objects like UITextViews. You should save the text properties of your text views instead.
If you want to manage a vertical stack of UITextView objects, I suggest adding a vertical stack view to your user interface, and then writing code that adds or removes UITextView subviews to your stack view. You should be able to find plenty of examples of adding and removing objects from stack views online. (It's really easy.)
If you want to manage a scrolling list of feeds of arbitrary length, you might want to use a table view or collection view instead. Those require that you set up a data model and implement a "data source". That takes a little practice to get right, but is very powerful.

Get a error claiming my 2D array is not 2D in impyute

Here is the shape of my array
b = data[0].values
print(b.shape)
(5126, 4229)
I get this error when I run this code:
from impyute.imputation.cs import mice
# start the MICE training
a=mice(b)
Error:
ValueError: Expected 2D array, got 1D array instead:
array=[].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
I am confused by this error message, any recommendations?
First, you must change your input data to a 2D array, so you must specify the number of features in your data using reshape function.
Please try to use b.reshape(5126, 4229), if not try to follow this example until you figure out the problem

kotlin add elements in array as sections

Can anyone teach me how to initiate/add items in array as section?
something like - array[["john"],["daniel"],["jane"]]
tried a few like var d = ArrayList<CustomClass>(arrayListOf<CustomClass>())
that does not work.
for example, if i add "david" i want it look like array[["john"],["daniel", "david"],[“keith"]] by finding the index of array contains letter "d"
how can i display them on a custom base adapter / listview? currently using a viewHolder
val viewHolder = ViewHolder(row.name) to display.
Thanks!
If I am right, you're trying to create an ArrayList that contains other ArrayLists. So writing:
var d = ArrayList<CustomClass>(arrayListOf<CustomClass>())
Won't work because the type of the main ArrayList is not String, but ArrayList. So you need to write:
var names = ArrayList<ArrayList<String>>()
names.add(arrayListOf("jane", "john))
Regarding the second question, check out this course that has a section about RecyclerView: https://classroom.udacity.com/courses/ud9012 If you are a beginner, I strongly encourage you to follow the whole tutorial.

Swift 3 - set the key when appending to array

I have this array where I set the keys on the creation. Now in some point in my view I load some more information based on ids (the keys).
var colors = [
"37027" : UIColor(red:150/255, green:57/255, blue:103/255, alpha:1),
"12183" : UIColor(red:234/255, green:234/255, blue:55/255, alpha:1),
"44146" : UIColor(red:244/255, green:204/255, blue:204/255, alpha:1)
]
I want to add more colors to this array dynamically. How can I insert new items in the array setting the key? Something like
colors["25252"] = UIColor(red:244/255, green:204/255, blue:204/255, alpha:1)
The line above doesn't work, it is just to illustrate what I need.
Thanks for any help
Update: the code above is an example. Below the real code:
var placedBeacons : [BeaconStruct] = []
BeaconModel.fetchBeaconsFromSqlite(completionHandler: {
beacons in
for item in beacons{
self.placedBeacons["\(item.major):\(item.minor)"] = item
}
})
Error: Cannot subscript a value of type '[BeaconStruct]' with an index of type String
To match the key subscripting
self.placedBeacons["\(item.major):\(item.minor)"] = item
you have to declare placedBeacons as dictionary rather than an array
var placedBeacons = [String:BeaconStruct]()
It requires that item is of type BeaconStruct
The code you wrote, it should work. I have used such kind of code and was able to implement successfully. I just tested your code in my end and it's working for me. I declared colors variable globally in my class file and in view did load method added the second code to add another item in my colors array. After printing it out. My output shows full list of array with 4 items and the number of array count return 4 as well.
Please let me know, more details of your scenario so i can help you to figure it out the issue. but looks like it should work.

Matlab: Unable to store information into array

Im trying to store values from radialspeed (a function from the phased array toolbox) into an array, but im getting errors:
Conversion to cell from double is not possible.
Error in modelCar (line 40)
Cell(1,T)= Rspeed;
^^Error Message
Cell = cell(1,12)
for T = 1:11
[POS,v] = step(H,T);
Rspeed = radialspeed(POS,v,[25; 25; 70],[0; 0; 0]);
typecast(Rspeed,'uint16');
Cell(1,T)= Rspeed;
%%Rspeed = Vel.Radspeed(:,T);
disp(Rspeed);
end
^^^Excerpt of the code im using.
Another question any tips to plot a graph continuously while in the loop, the draw now function doesn't seem to work
Thank you.
You should not use Cell as a variable since cell is reserved keyword in MATLAB. Though using Cell will pose no problems, but a simple typing mistake can inject errors into your code. You may use myCell, R_cell etc.
By writing, Cell(1,T)= Rspeed you are trying to assign Rspeed of type double to a cell datatype. You should write Cell{1,T}=Rspeed or Cell{T}=Rspeed. You can also visualize your output for each iteration as follows:
Replace disp(Rspeed) by:
hold on;scatter(T,Rspeed,'ro');
pause(0.001);
1st question: Cell(1,T) will return a cell so you need to change your code to Cell{T}= Rspeed;.
2nd question: recall plot is a possible solution if speed is not a main concern.

Resources