Azzera filtri
Azzera filtri

apply text values to elements in a vector

12 visualizzazioni (ultimi 30 giorni)
Muneer
Muneer il 30 Ott 2013
Commentato: Cedric il 31 Ott 2013
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!

Risposta accettata

Cedric
Cedric il 30 Ott 2013
Modificato: Cedric il 30 Ott 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 Commenti
Muneer
Muneer il 31 Ott 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.
Cedric
Cedric il 31 Ott 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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by