apply text values to elements in a vector

4 次查看(过去 30 天)
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!

采纳的回答

Cedric
Cedric 2013-10-30
编辑:Cedric 2013-10-30
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 个评论
Muneer
Muneer 2013-10-31
I wanted to be able to save the column of "text labels" so that I can display it again and verify accuracy in the next phase of the code that I'm working on. If I could type in the variable name, hit enter and have the entire column show up, it would make things a lot easier. Do you mean store them directly in the file created by fprintf? That may also be an acceptable option.
Cedric
Cedric 2013-10-31
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by