apply text values to elements in a vector

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!

 Respuesta aceptada

Cedric
Cedric el 30 de Oct. de 2013
Editada: Cedric el 30 de Oct. de 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 comentarios

Muneer
Muneer el 30 de Oct. de 2013
Thanks for the help, Cedric. If I had a 0 in my "actions" vector, when I assigned my labels in the "labels" vector, would the first label be for 0 or for 1?
You're welcome. Indices in MATLAB start at 1, so you have to either add a condition in the FOR loop, e.g.
for k = 1 : numel( actions )
if isnan( actions(k) )
fprintf( 'NaN\tnot moving\n' ) ;
elseif actions(k) == 0
fprintf( '0\tsome text\n' ) ;
else
fprintf( '%d\t%s\n', actions(k), labels{actions(k)-3} ) ;
end
end
Or you can just add 1 to actions and change NaN values into the first available integer > 1 which is not in regular actions.
Muneer
Muneer el 30 de Oct. de 2013
Thanks this seems to be working well. Can fprint be used to save the displayed column in its own vector? If not, I'm trying to define an output vector and have it write to the vector in the for loop. However, I'm confused about how to write string values within a vector.
Cedric
Cedric el 30 de Oct. de 2013
Editada: Cedric el 30 de Oct. de 2013
What would be the purpose? Any string is a vector of characters, or character array. If you wanted to save multiple strings (with various lengths), you could use a cell array. If the purpose is to create a log file, however, you can avoid saving these strings in variables but store them directly in the file.
Muneer
Muneer el 31 de Oct. de 2013
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.
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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 30 de Oct. de 2013

Comentada:

el 31 de Oct. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by