I have kt containing data type "strings", one of them is "grad"i.e kt(1,1)=grad which is a string. I am also trying to get the content of the structure(Data.edata.grad) in my data using the string data set by doing Data.edata.kt(1,1) but i am getting the error kt(1,1) is not an existing field. How can i convert a string to a structure or how can i use my string data set to get the structures?

3 Kommentare

James Tursa
James Tursa am 10 Aug. 2018
Show us the actual code, not your description of it. And copy & paste the entire error message for us to see.
samuel okeleye
samuel okeleye am 11 Aug. 2018
Bearbeitet: Walter Roberson am 11 Aug. 2018
clear;
clc;
load('Unit 1001 Session 0068.mat')
k=Data.ECU_Data;
m=Data.Continuous_Data;
%CREATING THE FIELDS THAT NEED TO BE CHECKED
fieldcheck={'spn00084_src00_wheel_based_vehicle_speed';...
'spn00190_src00_engine_speed';...
'spn00102_src00_engine_intake_manifold_1_pressure';...
'spn00105_src00_engine_intake_manifold_1_temperature';...
'spn00108_src00_barometric_pressure';...
'spn00513_src00_actual_engine___percent_torque';...
'spn00110_src00_engine_coolant_temperature';...
'spn00183_src00_engine_fuel_rate';...
'spn00514_src00_nominal_friction___percent_torque';...
'spn00528_src00_engine_speed_at_point_2_engine_configuration';...
'spn00529_src00_engine_speed_at_point_3_engine_configuration';...
'spn00530_src00_engine_speed_at_point_4_engine_configuration';...
'spn00531_src00_engine_speed_at_point_5_engine_configuration';...
'spn00540_src00_engine_percent_torque_at_point_2_engine_configur';...
'spn00541_src00_engine_percent_torque_at_point_3_engine_configur';...
'spn00542_src00_engine_percent_torque_at_point_4_engine_configur';...
'spn00543_src00_engine_percent_torque_at_point_5_engine_configur';...
'spn00544_src00_engine_reference_torque_engine_configuration';...
'spn04154_src00_actual_engine___percent_torque_high_resolution';...
'spn03700_src00_diesel_particulate_filter_active_regeneration_st'};
fieldcheck2={'ambient.temperature';...
'vehicle.speed.gps';...
'vehicle.position.latitude';...
'vehicle.position.longitude';...
'vehicle.position.altitude'};
%%Channel Existence Check
af1=isfield(k,{'spn00084_src00_wheel_based_vehicle_speed';...
'spn00190_src00_engine_speed';...
'spn00102_src00_engine_intake_manifold_1_pressure';...
'spn00105_src00_engine_intake_manifold_1_temperature';...
'spn00108_src00_barometric_pressure';...
'spn00513_src00_actual_engine___percent_torque';...
'spn00110_src00_engine_coolant_temperature';...
'spn00183_src00_engine_fuel_rate';...
'spn00514_src00_nominal_friction___percent_torque';...
'spn00528_src00_engine_speed_at_point_2_engine_configuration';...
'spn00529_src00_engine_speed_at_point_3_engine_configuration';...
'spn00530_src00_engine_speed_at_point_4_engine_configuration';...
'spn00531_src00_engine_speed_at_point_5_engine_configuration';...
'spn00540_src00_engine_percent_torque_at_point_2_engine_configur';...
'spn00541_src00_engine_percent_torque_at_point_3_engine_configur';...
'spn00542_src00_engine_percent_torque_at_point_4_engine_configur';...
'spn00543_src00_engine_percent_torque_at_point_5_engine_configur';...
'spn00544_src00_engine_reference_torque_engine_configuration';...
'spn04154_src00_actual_engine___percent_torque_high_resolution';...
'spn03700_src00_diesel_particulate_filter_active_regeneration_st'});
af2=isfield(m,{'ambient.temperature';...
'vehicle.speed.gps';...
'vehicle.position.latitude';...
'vehicle.position.longitude';...
'vehicle.position.altitude'});
%Available and Unavailable Channels
af1av=fieldcheck(af1==1);
af1unav=fieldcheck(af1==0);
%
af2av=fieldcheck2(af2==1);
af2unav=fieldcheck2(af2==0);
avk=k.af1av
Error
Reference to non-existent field 'af1av'.
Error in QA_QC (line 86)
avk=k.af1av;
samuel okeleye
samuel okeleye am 11 Aug. 2018
I switched the string to cell for simplicity.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 11 Aug. 2018

0 Stimmen

Data.edata.(kt(1,1))
You might perhaps need to use
Data.edata.(kt{1,1})

5 Kommentare

samuel okeleye
samuel okeleye am 11 Aug. 2018
This worked for a single entry. However i have 19 rows and 1 column and it did not work for the rest. I have also changed the data type from string to a different data type for simplicity. Thanks for your assistance
Note that isfield() returns logical directly, so
af1av=fieldcheck(af1==1);
af1unav=fieldcheck(af1==0);
can be written as just
af1av = fieldcheck(af1);
af1unav = fieldcheck(~af1);
Also, you do not need to code those text strings twice: you can
af1 = isfield(k, fieldcheck);
Anyhow, we have as outsider viewers have no reason to expect that af1av will be a scalar cell array containing only one character vector, so k referenced with fields af1av would be expected to referencing multiple fields. You cannot use the .(field) dynamic fieldname syntax to access multiple fields.
You should consider a different approach such as
kcell = struct2cell(k);
af1 = cell2struct( kcell(af1), af1av, 1); %not sure that 1 is correct here, possibly 2 instead.
samuel okeleye
samuel okeleye am 11 Aug. 2018
Bearbeitet: Walter Roberson am 11 Aug. 2018
Thanks for your help. Because k contains a lot of structures, out of which i am selecting few,kcell(af1) only selected the first set of structures within k, through the length of af1. I tried the method below and it worked. Please advise if you think my method is complicated. Thanks
for i=1:length(af1av)
af1avchannel(i)=k.(af1av{i,1});
end
Walter Roberson
Walter Roberson am 11 Aug. 2018
That code relies upon the fields all being the same datatype and all scalars (either numeric scalars or scalar logical or scalar character or scalar cell array.)
samuel okeleye
samuel okeleye am 11 Aug. 2018
Thank you.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by