How can I use a user input to pull a column from a timetable?
Show older comments
I am trying to pull one column of data using from a timetable using a user input. I can easily pull one column from a timetable using the raw code, but I cannot get it to work with a user input. Here's a snippet of code:
RawTime=[seconds(T.Time)];
prompt1='Enter the EXACT name of variable to view (ie. status_State):';
%%idk=input(prompt1,'s'); ignore this line
TM=T.input(prompt1,'s');
Matrixx=[RawTime,TM];
Everytime I try the above method I get a reasonable error: Unrecognized table variable name 'input'. Which does and doesnt make sense to me, because i thought it would use the input of the user rather than the literal word input.
Now I can get the "status_State" column manually if I do this:
RawTime=[seconds(T.Time)];
%%prompt1='Enter the EXACT name of variable to view (ie. status_State):';
%%idk=input(prompt1,'s'); ignore this line
TM=T.status_State;
Matrixx=[RawTime,TM];
But I want to make that a user input since there are multiple columns that a user should be able to choose from.
1 Comment
The MATLAB documentation shows how:
You should use either of these:
F = 'VarName';
T.(F)
T{:,F}
More complex code should be avoided.
Accepted Answer
More Answers (1)
Davide Masiello
on 4 Feb 2022
Edited: Davide Masiello
on 4 Feb 2022
Not sure this is the most elegant solution, but it does work
RawTime = [seconds(T.Time)];
prompt1 = 'Enter the EXACT name of variable to view (ie. status_State):';
idk = input(prompt1,'s');
index = find(contains(T.Properties.VariableNames,idk));
TM = T{:,index};
Matrixx = [RawTime,TM];
On a different aspect: when you use 'seconds', you assign the class duration to RawTime. When creating Matrixx, TM gets also converted into duration, as you can't have elements of different classes in an array. Be sure that's fine for you before proceeding.
1 Comment
Maksim Sorin
on 4 Feb 2022
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!