MATLAB struct array field access - arrays

I have an array of structures in MATLAB and I want to plot all of the values for one key. I know you can print it by doing array.key but for some reason hist(array.key) doesn't work.
Seems simple enough but I couldn't find how to do this.

clear all
array(10).key = 1:10;
mat = [array.key]
hist(mat)
Try something like the above.

Related

How can I auto fill a formula with dynamic value in array in the same cell?

so I have a formula inside an array that looks like this
={CRYPTOFINANCE("kraken:"&ARL8&"/USD", "price_history", "10d")}
I want to auto fill the array with the same function but with the 'ARL8' reference in an ascending order, just like auto filling cells, but keep it all in one cell it should look like this...
={CRYPTOFINANCE("kraken:"&ARL8&"/USD", "price_history", "10d") ; CRYPTOFINANCE("kraken:"&ARL9&"/USD", "price_history", "10d") ; CRYPTOFINANCE("kraken:"&ARL10&"/USD", "price_history", "10d")} ,SUBSTITUTE(TRANSPOSE(SPLIT(REPT(12,1050),2)),1,"MISTAKE")) etc.
the thing is that I have about 1000 values from ARL8 reference to ARL1008 reference so it will take really long for me to write it all manually, so is there a way I can keep the rest of the function but have the ARL cell reference dynamically written while being able to specify the length of the array?
It's something I am struggling for a while now and still couldn't find a way so I really appreciate if you could explain the solution as well, and if there is more info I can give let me know, and thanks.
I tried a few things but they gave me different errors I didn't know how to solve and others just didn't work, I will just put it in case it helps
=ARRAYFORMULA(CRYPTOFINANCE("kraken:"&ARL8:ARL1008&"/USD", "price_history", "10d"))
the error it gave me is - "error Attribute price_history isn't supported in batch formulas"
try:
=BYROW(ARL8:ARL20, LAMBDA(x, CRYPTOFINANCE("kraken:"&x&"/USD", "price_history", "10d"))
update
you can generate a formula with a formula like:
={""; INDEX("=ARRAYFORMULA({SPLIT(""Exchange,Base,Quote,Time,Open,High,Low,Close,Quote Volume,Base Volume"", "","")"&
QUERY(";QUERY(TO_TEXT(CRYPTOFINANCE(""kraken:""&"&C2&
SEQUENCE(C3, 1, C4)&"&""/USD"",""price_history"",""10d"")), ""offset 1"", )",,9^9)&"})")}
demo sheet

Using N-D interpolation with a generic rank?

I'm looking for an elegant way of useing ndgrid and interpn in a more "general" way - basically for any given size of input and not treat each rank in a separate case.
Given an N-D source data with matching N-D mesh given in a cell-array of 1D vectors for each coordinate Mesh={[x1]; [x2]; ...; [xn]} and the query/output coordinates given in the same way (QueryMesh), how do I generate the ndgrid matrices and use them in the interpn without setting a case for each dimension?
Also, if there is a better way the define the mesh - I am more than willing to change.
Here's a pretty obvious, conceptual (and NOT WORKING) schematic of what I want to get, if it wasn't clear
Mesh={linspace(0,1,10); linspace(0,4,20); ... linsapce(0,10,15)};
QueryMesh={linspace(0,1,20); linspace(0,4,40); ... linsapce(0,10,30)};
Data=... (whatever)
NewData=InterpolateGeneric(Mesh,QueryMesh,Data);
function NewData=InterpolateGeneric(Mesh,QueryMesh,Data)
InGrid=ndgrid(Mesh{:});
OutGrid=ndgrid(QueryMesh{:});
NewData=interpn(InGrid{:},Data,OutGrid{:},'linear',0.0)
end
I think what you are looking for is how to get multiple outputs from this line:
OutGrid = ndgrid(QueryMesh{:});
Since ndgrid produces as many output arrays as input arrays it receives, you can create an empty cell array in this way:
OutGrid = cell(size(QueryMesh));
Next, prove each of the elements of OutGrid as an output argument:
[OutGrid{:}] = ndgrid(QueryMesh{:});

How to assign Cell contents to a non-cell array object in MATLAB

The Code snippet looks like this:
legendentry=zeros(1, NumberOfFiles);
legendentry{1}= sprintf('Experimental');
legendentry{NumberOfFiles} = sprintf('Variable: %.2f', 0.5+(NumberOfFiles-1)*0.5);
h =legend(legendentry,'Location','southeast');
And if i run it like this it gives this error message: "Cell contents assignment to a non-cell array object."
If i leave out the first line legendentry=zeros(1, NumberOfFiles); the Code works fine for me and the Legend looks like i want it to be:
Experimental
Variable: 0.5
Variable: 1 .. and so on..
But then the legendentry is not anymore preallocated. So i want to know how i could preallocate the Array correctly if i needed to do that later..
I know that there has to be something with cellstr or num2str, but i donĀ“t know how to solve it myself.
Hope you guys can help me with this basic issue..
Preallocate legendentry as a cell array since you're going to assign it cells.
legendentry = cell(1, NumberOfFiles);

Using jsonencode with length 1 array

When using the MATLAB jsonencode function it seems very difficult to convert size 1 arrays into the correct JSON format i.e. [value]. For example if I do:
jsonencode(struct('words', [string('hello'), string('bye')]))
Then this produces:
{"words":["hello","bye"]}
which is correct. If however I do:
jsonencode(struct('words', [string('hello')]))
Then it produces:
{"words":"hello"}
losing the square brackets, which it needs because it is in general an array. The same happens when using a cell rather than an array, although using a cell does work if it's not inside a struct.
Any idea how I can work around this issue?
It seems this can be solved by using a cell rather than an array and then not creating the struct inline. Like
s.words = {'hello'};
jsonencode(s)
Output:
{"words":["hello"]}
I presume when created inline the cell functionality of matlab is actually trying to make multiple structs rather than multiple strings. Note that this still won't work with arrays as matlab treats a size one array as a scalar.

How do I assign a variable to refer to an array?

Disclaimer: I am pretty much a novice to Swift so prepare to cringe.
I have a series of arrays set out, when a user taps a button I would like one of these arrays to be called so I can use it in a function. They are named with thisArray then a number, i.e: thisArray1, thisArray2, etc.
I cannot find a way to make this work, the closest I have come to what I want is shown below but as you can all tell this definitely does not work.
var currentArray = "thisArray" + selectedArrayNumber
The outcome of this is to use the variable like below:
button1.setTitle(currentArray[1], for .normal)
If any of you can shed some light on this situation and tell me how badly I have gone wrong that would be much appreciated.
Create an array of your arrays, and then retrieve it using the selectedArrayNumber.
let arrays = [thisArray1, thisArray2, etc]
let currentArray = arrays[selectedArrayNumber]

Resources