Matlab: Unable to store information into array - arrays

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.

Related

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

Chef - Ruby - How to extract values from a nested array/list

I'm using this json content (I'm open to suggestions on better formatting here):
{"forwardingZones": [{"name": "corp.mycompany.com","dnsServers": ["192.168.0.1","192.168.0.2"]}]}
Note: we may add more items to this list as we scale out, both more IPs and more names, hence the "join(',')" in the end of the code below.
And I'm trying to loop through it to get this result:
corp.mycompany.com=192.168.0.1;192.168.0.2
Using this code:
forward_zones = node['DNS']['forward_zones'].each do |forwarded_zone|
forwarded_zone_name = forwarded_zone['name']
forwarded_zone_dns_servers = forwarded_zone['dns_servers'].join(';')
"#{forwarded_zone_name}=#{forwarded_zone_dns_servers}"
end.join(',')
This is the result that I get:
{"dnsServers"=>["192.168.0.1", "192.168.0.2"], "name"=>"corp.mycompany.com"}
What am i doing wrong...?
x.each returns x. You want x.map.

Creating pie chart with array

I am trying to create a Pie chart based on an array (rather than a range). The array is [11,10,1] (I have other code that populates the array).
Dim type_chart As Chart
Dim type_array(2) As Integer
Set type_chart = Charts.Add
type_chart.ChartType = xlPie
type_chart.SeriesCollection(1).Values = type_array
On the last line of the code above, I receive an 'Invalid Parameter' error.
Also, it doesn't have to use an array, but it cannot use a Range.
Your chart needs to work from a range. Find a blank area you can use. Try something like the following:
my_temp_range = "A10:C10"
ActiveSheet.Range(my_temp_range) = type_array
type_chart.SeriesCollection(1).Values = ActiveSheet.Range(my_temp_range)
Once you're working with a temporary range you may not even need the last line (so long as the size of the array doesn't change). You could just set up the chart in advance instead.

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

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.

setting object properties value for object array in matlab

I have created an array of objects and I would like assign a property value in a vector operation without using a for loop. Unfortunately I get an error.
A simplified example of the problem.
classdef clsMyClass < handle
properties
dblMyProperty1
end
methods
function obj = clsMyClass()
end
end
end
And when running
vecMyArray = clsMyClass.empty(100,0);
vecMyArray(100) = clsMyClass;
vecMyArray.dblMyProperty1 = 1:100;
We get the following error:
??? Incorrect number of right hand side elements in dot name
assignment. Missing [] around left hand side is a likely cause.
Any help would be appreciated.
I see what you're trying to do now. Use disperse from the MATLAB File Exchange:
>> [vecMyArray.dblMyProperty1] = disperse(1:100);
>> vecMyArray(1).dblMyProperty1
ans =
1
>> vecMyArray(10).dblMyProperty1
ans =
10
You can use the deal function for exactly this purpose:
[vecMyArray.dblMyProperty1] = deal(1:100);
See: http://www.mathworks.com/company/newsletters/articles/whats-the-big-deal.html
Edit: No you can't, actually; that'll set them to all be the vector 1:100.
I think you'll find your answer here in "Struct array errors." Even though this is a class, similar rules apply.
Unfortunately missing [] is not the cause, since adding them causes more errors. The cause is that you cannot assign the same value to all fields of the same name at once, you must do it one at a time, as in the following code:
So you'll need:
for ii=1:100
vecMyArray(ii).dblMyProperty1 = ii;
end
I know it's not satisfying, but I think it at least helps us to definitively understand this error.

Resources