Azzera filtri
Azzera filtri

Hi, there is a (1x6) vector that i want to extract values from. I used strings to pick-up the subtitle and now the problem is that i have to extract every value under the subtitle (e.g Oxygen). How to i extract those values without hardcoding ?

1 visualizzazione (ultimi 30 giorni)
InputData;
strmeasurements = Measurements;
'Oxygen';
strcmp(strmeasurements,'Oxygen')
for = 1:1:length(strmeasurements)
if strcmp(strmeasurements,'Oxygen') > 0
  3 Commenti
WILLBES BANDA
WILLBES BANDA il 27 Mar 2020
InputData is a script that has the values.(shown below)
"Oxygen" "CarbonDioxide" "Temperature" "Pressure" "SolarRadiation"
"19.126323" "2316.074" "20.22159" "100.55903" "5.5317028e-05"
"21.649558" "1675.8466" "19.700265" "97.258761" "4.7879645e-05"
"21.365389" "5137.8534" "17.95304" "100.52854" "2.3418843e-05"
"20.849494" "4038.5941" "20.266467" "67.042816" "6.0612875e-05" "
"20.435402" "1735.1865" "22.257325" "74.053206" "4.8704271e-05"
"23.822268" "3127.1723" "21.121039" "75.086682" "1.9341138e-05"
"19.171776" "2664.9685" "18.31899" "68.216119" "7.7129851e-05"
"19.860289" "2044.0518" "17.646104" "88.961919" "4.3741877e-05"
"21.355073" "2823.8126" "18.492655" "72.283582" "6.6637808e-05"
Hardcoding includes the code below because the order can be changed when testing if the code works (e.g the matrix can start by pressure instead of Oxygen, then the values will be wrong)
InputData;
measurements = strtok(Measurements);
A = measurements;
for measurements = strtok(Measurements)
Oxygen = A(:,1);
end
for measurements = strtok(Measurements)
CarbonDioxide = A(:,2);
end
for measurements = strtok(Measurements)
Temperature = A(:,3);
end
for measurements = strtok(Measurements)
Pressure = A(:,4);
end
for measurements = strtok(Measurements)
SolarRadiation = A(:,5);
end
for measurements = strtok(Measurements)
Humidity = A(:,6);
end
I would like to extract those values under the subtitles Oxgen, Temperature and pressure

Accedi per commentare.

Risposta accettata

Rik
Rik il 27 Mar 2020
It would probably make a more sense to use the table data type instead of a string array that is parsed to separate variables.
a=["Oxygen" "CarbonDioxide" "Temperature" "Pressure" "SolarRadiation"
"19.126323" "2316.074" "20.22159" "100.55903" "5.5317028e-05"
"21.649558" "1675.8466" "19.700265" "97.258761" "4.7879645e-05"
"21.365389" "5137.8534" "17.95304" "100.52854" "2.3418843e-05"
"20.849494" "4038.5941" "20.266467" "67.042816" "6.0612875e-05"
"20.435402" "1735.1865" "22.257325" "74.053206" "4.8704271e-05"
"23.822268" "3127.1723" "21.121039" "75.086682" "1.9341138e-05"
"19.171776" "2664.9685" "18.31899" "68.216119" "7.7129851e-05"
"19.860289" "2044.0518" "17.646104" "88.961919" "4.3741877e-05"
"21.355073" "2823.8126" "18.492655" "72.283582" "6.6637808e-05"];
for col=1:size(a,2)
L=ismember(a(1,:),a(1,col));
data=str2double(a(2:end,L));
switch a(1,col)
case "Oxygen"
Oxygen=data;
case "CarbonDioxide"
CarbonDioxide=data;
case "Temperature"
Temperature=data;
case "Pressure"
Pressure=data;
case "SolarRadiation"
SolarRadiation=data;
otherwise
%deal with unexpected input here
error('data type not recognized')
end
end

Più risposte (0)

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by