How to convert a string array into floats in PHP - string

I'm selecting some Strings from my SQLite DB and store them in an Array. Now I want to plot the values with the framework "Razorflow". I think it is only possible if the values of the array are floats, am I wrong?
In my DB I'm storing temperature and humidity of 12 different sensor nodes, in this form:
id|temperature|humidity|...|...
1 | 22.50C| 47.50%|...|...
..
I heared something about the floatval()-function, I also heared that this function is not made for objects like Arrays. Is there a simple solution? I'm very new to PHP :-D

Depends on how you're getting the values back from the database.
id|temperature|humidity|...|...
Sounds like a string to me, so i would first explode it into an array and then iterate the array casting floatval into every element.
What you do after that process depends on you.
EDIT:
If your query is:
$humi = $database->query((SELECT humidity FROM Measurement WHERE topic_hum='WSN9/humi'
You will get back only 1 column (humidity) and 0 or more rows depending on your database. Let's say it's only 1 for now:
$resul = mysql_query($humi,$link);
$rows = mysql_fetch_array($resul);
$myarray = explode("|", $rows["humidity"]);
This should give us an array called myarray containing X elements each with a "single" string part value. So we can iterate over it and parse it. There is also the shorthand "array_map" to iterate over an array using a callback function over each element and returning the value as an array:
$myparsedarray = array_map('floatval', $myarray)
Now you have an array with only float values (and errors maybe, check your data!)

Related

Nodejs - Read values of array as number instead of string

I created an array inside my own class in nodejs to store real values.
this.values = new Array();
and almost every second a new value is pushed to this array.
this.values.push(Number(dataset.c).toFixed(4));
Finaly I have a function where I need to provide the array in the argument list to do bollinger band calculation. This function need an array filled with real or integer values but in my array every value is stored as an string.
Is it possible to change this storage behaviour or how can I 'typecast' all the values in this array?
Best regards
Found a solution using the map-Function.
this.values.map(Number)
did the job.

Map repeated values in presto

I'm extracting data from JSON and mapping two arrays in presto.It works fine when there are no repeated values in the array but fails with error - Duplicate map keys are not allowed if any of the values are repeated.I need those values and cannot remove any of the values from the array.Is there a work around for this scenario?
Sample values:
array1 -- [Rewards,NEW,Rewards,NEW]
array2 -- [losg1,losg2,losg3,losg4]
Map key/value has to be generated like this [Rewards=>losg1,NEW=>losg2,Rewards=>losg3,NEW=>losg4]
Pairs of associations can be returned like this:
SELECT ARRAY[ROW('Rewards', 'losg1'), ROW('NEW', 'losg2'), ROW('Rewards', 'losg3')]

count occurrences of a string in a structure

I have a structure mydata and I need to access one of its fields mydata.myfield, and within that field, access another field mydata.myfield.mysecondfield. In the last field, mydata.myfield.mysecondfield I need to check how many times a particular string ('apple') occurs.
I have tried with:
aaa=unique(mydata.myfield.mysecondfield,'apple')
bbb=cellfun(#(x) sum(ismember(mydata.myfield.mysecondfield,x)),aaa,'un',0)
but I get this error: Attempt to reference field of non-structure array.
The structure contains fields with both strings and numeric values.
The underlying problem may be due to the fact that the structure is a little bit different from how you describe it. Following your question, I created it as follows:
mydata = struct();
mydata.myfield.mysecondfield = {'apple' 'apple' 'orange' 'banana' 'apple' 'kiwi'};
and since I'm not getting the same error you get, I think the underlying types may be slightly mismatching. Anyway, given mydata defined as above, if you change your code as follows, it should work but it will return the count of every unique occurrence within the field:
aaa = unique(mydata.myfield.mysecondfield);
bbb = cellfun(#(x) sum(ismember(mydata.myfield.mysecondfield,x)),aaa,'un',0)
bbb =
4×1 cell array
[3]
[1]
[1]
[1]
If you only want to count the number of apple occurrences, you should use the following approach instead:
apple_count = sum(strcmp(mydata.myfield.mysecondfield,'apple')); % 3

Calling a vector from a string

I am attempting to write an algorithm that selects a specific reference standard (vector) as a function of temperature. The temperature values are stored in a structure ( procspectra(i).temperature ). My reference standards are stored in another structure ( standards.interp.zeroed.ClOxxx ) where xxx are numbers such as 200, 210, 220, etc. I have built the rounding construct and paste it below.
for i = 1:length(procspectra);
if mod(-procspectra(i).temperature,10) > mod(procspectra(i).temperature,10);
%if mod(-) > mod(+) round down, else round up
tempvector(i) = procspectra(i).temperature - mod(procspectra(i).temperature,10);
else
tempvector(i) = procspectra(i).temperature + mod(-procspectra(i).temperature,10);
end
clostd = strcat('standards.interp.zeroed.ClO',num2str(tempvector(i)));
end
This construct works well. Now, I have built a string which is identical to the name of the vector I want to invoke, but I'm uncertain how to actually call the vector given that this is encoded as a string. Ideally I want to do something within the for-loop like:
parameters(i).standards.ClOstandard = clostd
where I actually am assigning that parameter structure to be the same as the vector I have saved in the standards structure I have previously generated (and not just a string)
Could anyone help out?
Don't construct clostd like that (containing the full variable name), make it contain only the last field name instead:
clostd = ['ClO' num2str(tempvector(i))];
parameters(i).standards.ClOstandard = standards.interp.zeroed.(clostd);
This is the syntax of accessing a structure's field dynamically, using a string. So the following three are equivalent:
struc.Cl0123
struc.('Cl0123')
fieldn='Cl0123'; struc.(fieldn)

an efficient way acess a range in a user defined type?

I've got a user defined type with about 5000 entries. I would like to select a range of the data in about 1,000 entry blocks and use it as an array. Is there a way to do this without looping?
Something like
MyArray = MyType(1:1000).property
rather than
for i = 1 to 1000
MyArray(i) = MyType(i).property
Next i
Thanks!
No, there is no way to convert a collection of elements into an array without looping or special accessor methods for the type - let alone convert a common property from a collection of elements into an array.
The only objects that support anything like this are Range objects, for which you can convert certain properties of an array of cells (range) into an array using:
MyArray = Range("A1:A1000").Value
Again, nothing like this is available for other types unless the programmer goes through the trouble of defining the behaviour - and even if they did define it, odds are the method would likely include iterating over the elements anyways, just within the type class.

Resources