apply text values to elements in a vector
5 views (last 30 days)
Show older comments
I have a vector that is a sequence of number and NaN values. Is there a way for me to save values for these numbers and NaN values and then have the entire vector display with those text values inserted? For example:
If my vector is the column on the left below, and my variables are defined as
5 = turn left
4 = turn right
6 = go straight
NaN = not moving
can I get it to display as the column on the right after saving the variables?
5 turn left
5 turn left
5 turn left
4 turn right
4 turn right
4 turn right
6 go straight
6 go straight
6 go straight
NaN not moving
5 turn left
Thanks in advance for the help!
0 Comments
Accepted Answer
Cedric
on 30 Oct 2013
Edited: Cedric
on 30 Oct 2013
If you are allowed to remap "not moving" to e.g. code 1, you can proceed as follows:
actions = [5,5,5,4,4,4,6,6,6,NaN,5] ;
labels = {'not moving', '', '', 'turn right', 'turn left', 'go straight'} ;
% - NaN -> 1.
actions(isnan(actions)) = 1 ;
% - Display.
for k = 1 : numel( actions )
fprintf( '%d\t%s\n', actions(k), labels{actions(k)} ) ;
end
With that, you get
5 turn left
5 turn left
5 turn left
4 turn right
4 turn right
4 turn right
6 go straight
6 go straight
6 go straight
1 not moving
5 turn left
If you don't want that remap (NaN->1), it is easy to use an IF statement in a loop for managing NaN entries..
actions = [5,5,5,4,4,4,6,6,6,NaN,5] ;
labels = {'turn right', 'turn left', 'go straight'} ;
for k = 1 : numel( actions )
if isnan( actions(k) )
fprintf( 'NaN\tnot moving\n' ) ;
else
fprintf( '%d\t%s\n', actions(k), labels{actions(k)-3} ) ;
end
end
With that, you get
5 turn left
5 turn left
5 turn left
4 turn right
4 turn right
4 turn right
6 go straight
6 go straight
6 go straight
NaN not moving
5 turn left
6 Comments
Cedric
on 31 Oct 2013
You could generate a long string using SPRINTF ..
actions = [5,5,5,4,4,4,6,6,6,NaN,5] ;
labels = {'not moving', '', '', 'turn right', 'turn left', 'go straight'} ;
actions(isnan(actions)) = 1 ;
temp = [num2cell(actions); labels(actions)] ;
report = sprintf( '%d\t%s\n', temp{:}) ;
With that, what was outputted to screen previously is now stored as a string in variable report, that you can display by typing its name in the command window.
A better option (up to me) would be to pack the code for printing the report in a function, and call the function when you want the report. This way you can easily get a dynamic report if actions evolve.
function displayReport( actions )
labels = {'not moving', '', '', 'turn right', 'turn left', ...
'go straight'} ;
actions(isnan(actions)) = 1 ;
for k = 1 : numel( actions )
fprintf( '%d\t%s\n', actions(k), labels{actions(k)} ) ;
end
end
Saving this in file displayReport.m, you can then call it passing actions from the command window at any time (or from your script)..
>> actions = [5,5,5,4] ;
>> displayReport( actions ) ;
5 turn left
5 turn left
5 turn left
4 turn right
and a moment later, with more actions having stacked in your actions buffer ..
>> actions = [5,5,5,4,4,4,6,6,6,NaN,5] ;
>> displayReport( actions ) ;
5 turn left
5 turn left
5 turn left
4 turn right
4 turn right
4 turn right
6 go straight
6 go straight
6 go straight
1 not moving
5 turn left
More Answers (0)
See Also
Categories
Find more on Characters and Strings 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!