Can I display two sprintf variables side by side?

4 views (last 30 days)
I have two functions that output certain variables and right now each has their own print statement to easily display these variable when the function is run. I would like to use these functions in a separate function and have that one display the sprintf variables for each of the functions printed statements side by side for comparison. Is there any easy way to do this without making a modified fprintf for all the displayed variables of each function? Here is an example of one of the sprintf variables:
print_state = 'Given values from data export\n\nStructure: PTV\n Mean Dose: %.1f\n Min Dose: %.1f\n Max Dose: %.1f\n Modal Dose: %.1f\n\nStructure: Rectum\n Mean Dose: %.1f\n Min Dose: %.1f\n Max Dose: %.1f\n Modal Dose: %.1f\n\nStructure: Bladder\n Mean Dose: %.1f\n Min Dose: %.1f\n Max Dose: %.1f\n Modal Dose: %.1f';
grabbed_values = sprintf(print_state,Mean_PTV,Min_PTV,Max_PTV,Modal_PTV,Mean_Rec,Min_Rec,Max_Rec,Modal_Rec,Mean_Blad,Min_Blad,Max_Blad,Modal_Blad);
I would like to use something like display([grabbed_values],[calculated_values]) to show each one sprintf statement next to the other
  2 Comments
Image Analyst
Image Analyst on 24 Jul 2015
Your "print_state" is the format specifier for the sprintf() that creates grabbed_values. Please put in some sample numbers and give a resulting output display that you'd like to see, so we can get an idea of what you're after.
k5jyw153vcxb
k5jyw153vcxb on 24 Jul 2015
This is sort of the format of what it prints out. calculated_values prints out something very similar. I would like to have calculated_values printed out in a column along side the grabbed_values print out so it's easy to compare the values
>> grabbed_values
grabbed_values =
Given values from data export
Structure: PTV
Mean Dose: 7975.8
Min Dose: 7271.9
Max Dose: 8377.1
Modal Dose: 8005.5
Structure: Rectum
Mean Dose: 2691.2
Min Dose: 232.1
Max Dose: 8234.2
Modal Dose: 383.0
Structure: Bladder
Mean Dose: 2113.5
Min Dose: 103.3
Max Dose: 8247.7
Modal Dose: 192.4

Sign in to comment.

Accepted Answer

Cedric
Cedric on 24 Jul 2015
Edited: Cedric on 24 Jul 2015
I would have the functions output numbers and no strings (which contain line returns), and then build a print function which is able to generate a table. The reason is that there is no easy way to put side by side strings when they don't all have the same width between all consecutive line returns.
If you don't want to update these functions so they output numbers or carefully trimmed arrays of characters (e.g. because you are not allowed bring modifications to their code), you can post-process the strings, e.g. by splitting them by line and adapting lengths or printing them using a padding.
To illustrate, imagine that we have 3 grabbed_values in a cell array (I generate a fake one):
print_state = 'Given values from data export\n\nStructure: PTV\n Mean Dose: %.1f\n Min Dose: %.1f\n Max Dose: %.1f\n Modal Dose: %.1f\n\nStructure: Rectum\n Mean Dose: %.1f\n Min Dose: %.1f\n Max Dose: %.1f\n Modal Dose: %.1f\n\nStructure: Bladder\n Mean Dose: %.1f\n Min Dose: %.1f\n Max Dose: %.1f\n Modal Dose: %.1f';
randValues = rand( 1, 12 ) ;
for k = 1 : 3
values = num2cell( randValues + 10*(k-1) ) ;
grabbed_values{k} = sprintf( print_state, values{:} ) ;
end
which gives:
>> grabbed_values
grabbed_values =
[1x282 char] [1x294 char] [1x294 char]
>> grabbed_values{1}
ans =
Given values from data export
Structure: PTV
Mean Dose: 0.2
Min Dose: 0.1
Max Dose: 0.1
Modal Dose: 0.6
Structure: Rectum
Mean Dose: 0.6
Min Dose: 0.4
Max Dose: 0.0
Modal Dose: 0.2
Structure: Bladder
Mean Dose: 0.5
Min Dose: 0.8
Max Dose: 0.8
Modal Dose: 0.9
>> grabbed_values{2}
ans =
Given values from data export
Structure: PTV
Mean Dose: 10.2
Min Dose: 10.1
Max Dose: 10.1
Modal Dose: 10.6
Structure: Rectum
Mean Dose: 10.6
Min Dose: 10.4
Max Dose: 10.0
Modal Dose: 10.2
Structure: Bladder
Mean Dose: 10.5
Min Dose: 10.8
Max Dose: 10.8
Modal Dose: 10.9
>> grabbed_values{3}
ans =
Given values from data export
Structure: PTV
Mean Dose: 20.2
Min Dose: 20.1
Max Dose: 20.1
Modal Dose: 20.6
Structure: Rectum
Mean Dose: 20.6
Min Dose: 20.4
Max Dose: 20.0
Modal Dose: 20.2
Structure: Bladder
Mean Dose: 20.5
Min Dose: 20.8
Max Dose: 20.8
Modal Dose: 20.9
We can do the following: first, we build a cell array of lines with one column per grabbed item:
nStr = numel( grabbed_values ) ;
for strId = 1 : nStr
lines = regexp( grabbed_values{strId}, sprintf('\n'), 'split' )' ;
allGrabbed(1:numel(lines), strId) = lines ;
end
where we use REGEXP because STRSPLIT would treat '\n\n' as a single '\n'. This builds the following cell array:
>> allGrabbed
allGrabbed =
'Given values from data export' 'Given values from data export' 'Given values from data export'
'' '' ''
'Structure: PTV' 'Structure: PTV' 'Structure: PTV'
' Mean Dose: 0.8' ' Mean Dose: 10.8' ' Mean Dose: 20.8'
' Min Dose: 0.5' ' Min Dose: 10.5' ' Min Dose: 20.5'
' Max Dose: 0.3' ' Max Dose: 10.3' ' Max Dose: 20.3'
' Modal Dose: 0.1' ' Modal Dose: 10.1' ' Modal Dose: 20.1'
'' '' ''
'Structure: Rectum' 'Structure: Rectum' 'Structure: Rectum'
' Mean Dose: 0.6' ' Mean Dose: 10.6' ' Mean Dose: 20.6'
' Min Dose: 0.2' ' Min Dose: 10.2' ' Min Dose: 20.2'
' Max Dose: 0.1' ' Max Dose: 10.1' ' Max Dose: 20.1'
' Modal Dose: 0.8' ' Modal Dose: 10.8' ' Modal Dose: 20.8'
'' '' ''
'Structure: Bladder' 'Structure: Bladder' 'Structure: Bladder'
' Mean Dose: 0.5' ' Mean Dose: 10.5' ' Mean Dose: 20.5'
' Min Dose: 0.7' ' Min Dose: 10.7' ' Min Dose: 20.7'
' Max Dose: 0.1' ' Max Dose: 10.1' ' Max Dose: 20.1'
' Modal Dose: 0.4' ' Modal Dose: 10.4' ' Modal Dose: 20.4'
Then we compute the max line length and we build a formatSpec with padding, leaving e.g. a 2 white spaces gap:
maxLength = max( max( cellfun( @length, allGrabbed ))) ;
fSpec = [repmat( sprintf( '%%-%ds', maxLength+2 ), 1, nStr), '\n'] ;
And finally we build the side by side string:
temp = allGrabbed' ;
sideBySide = sprintf( fSpec, temp{:} ) ;
and we get:
>> sideBySide
sideBySide =
Given values from data export Given values from data export Given values from data export
Structure: PTV Structure: PTV Structure: PTV
Mean Dose: 0.8 Mean Dose: 10.8 Mean Dose: 20.8
Min Dose: 0.5 Min Dose: 10.5 Min Dose: 20.5
Max Dose: 0.3 Max Dose: 10.3 Max Dose: 20.3
Modal Dose: 0.1 Modal Dose: 10.1 Modal Dose: 20.1
Structure: Rectum Structure: Rectum Structure: Rectum
Mean Dose: 0.6 Mean Dose: 10.6 Mean Dose: 20.6
Min Dose: 0.2 Min Dose: 10.2 Min Dose: 20.2
Max Dose: 0.1 Max Dose: 10.1 Max Dose: 20.1
Modal Dose: 0.8 Modal Dose: 10.8 Modal Dose: 20.8
Structure: Bladder Structure: Bladder Structure: Bladder
Mean Dose: 0.5 Mean Dose: 10.5 Mean Dose: 20.5
Min Dose: 0.7 Min Dose: 10.7 Min Dose: 20.7
Max Dose: 0.1 Max Dose: 10.1 Max Dose: 20.1
Modal Dose: 0.4 Modal Dose: 10.4 Modal Dose: 20.4
All this looks huge, but it is just a few lines (that I shortened a bit by swapping dimensions when we build allGrabbed), assuming that you have a cell array grabbed_values of strings to put side by side:
nStr = numel( grabbed_values ) ;
for strId = 1 : nStr
lines = regexp( grabbed_values{strId}, sprintf('\n'), 'split' ) ;
allGrabbed(strId, 1:numel(lines)) = lines ;
end
maxLength = max( max( cellfun( @length, allGrabbed ))) ;
fSpec = [repmat( sprintf( '%%-%ds', maxLength+2 ), 1, nStr), '\n'] ;
sideBySide = sprintf( fSpec, allGrabbed{:} ) ;
An alternative is probably to build a MATLAB table after the split, but I have little experience with printing tables.
  2 Comments
k5jyw153vcxb
k5jyw153vcxb on 24 Jul 2015
Edited: k5jyw153vcxb on 24 Jul 2015
I am able to edit the code of each of the functions but I don't have any experience with printing tables and I was hoping for an easier way than making some complicatedly long print statement. I don't fully understand the code you've provided for the splitting, but it does do the job I had hoped for. Thank you!
Cedric
Cedric on 24 Jul 2015
My pleasure! Drop me an email if you need to understand it better, because I am not checking the forum a lot anymore.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!