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

James Tursa
James Tursa 2018년 8월 10일
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 2018년 8월 11일
편집: Walter Roberson 2018년 8월 11일
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 2018년 8월 11일
I switched the string to cell for simplicity.

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2018년 8월 11일

0 개 추천

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

댓글 수: 5

samuel okeleye
samuel okeleye 2018년 8월 11일
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 2018년 8월 11일
편집: Walter Roberson 2018년 8월 11일
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 2018년 8월 11일
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 2018년 8월 11일
Thank you.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

제품

릴리스

R2018a

질문:

2018년 8월 10일

댓글:

2018년 8월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by