Display Text Without Formatting

41 views (last 30 days)
The following string displays in the command window formatted as a hyperlink. How do I display the entire contents of str without formatting?
>> str = '<test TEST>'
str =
TEST
  2 Comments
Daniel Shub
Daniel Shub on 1 Mar 2012
This is a really great question and MATLAB needs a way to turn off string formatting. Sometimes we want to know what is in our strings and not have them look pretty.
Jonathan
Jonathan on 7 Sep 2012
I am going with the following until a better and/or permanent solution is presented. Thanks to a Mathworker for motivating this idea!
str = '<test TEST>';
str = strrep(str, '=', sprintf('= \b'));
disp(str); % built-in
display(str); % built-in

Sign in to comment.

Accepted Answer

Andrew Newell
Andrew Newell on 1 Mar 2012
You can replace all the occurrences of href by HREF for display:
str = {'<test TEST1>';'<test TEST2>'};
dispStr = regexprep(str,'href','HREF');
disp(dispStr)
  2 Comments
Jan
Jan on 28 Aug 2012
STRREP is faster than REGEXPREP.

Sign in to comment.

More Answers (4)

Daniel Shub
Daniel Shub on 1 Mar 2012
What about corrupting the string with spaces and backspaces ...
str = '<test TEST>';
showstr = @(str)([sprintf('%c \b', str), sprintf('\n')]);
showstr(str)
This means that you can see all the content of your string without any formating (including formatting strings like \n) and don't have to guess what MATLAB is going to do.
  2 Comments
Jonathan
Jonathan on 1 Mar 2012
I accepted too soon. I like this answer the best. It is the most general.
This is an awkward situation to begin with. No wonder it requires such awkward code to circumvent. I would rather have BOTH: (1) a display function that does not format, and (2) a global setting to turn off all display formatting.
Thank you for this insightful solution, Daniel.
Jonathan
Daniel Shub
Daniel Shub on 29 Aug 2012
Sorry for not responding sooner. See my follow up answer which gives 1 and 2. Also, consider voting for answers that you think are helpful by clicking the triangle.

Sign in to comment.


Thomas
Thomas on 1 Mar 2012
How about this one
fprintf('%c \b',length(str)-1,str)
Output
<test TEST>
  1 Comment
Jonathan
Jonathan on 28 Aug 2012
This is an elegant improvement over Daniel's answer. Thank you.

Sign in to comment.


Daniel Shub
Daniel Shub on 29 Aug 2012
Edited: Daniel Shub on 29 Aug 2012
Can anyone eliminate the use of eval?
In a comment to my other answer, Jonathan said "I would rather have BOTH: (1) a display function that does not format, and (2) a global setting to turn off all display formatting." This can be achieved for strings with a little bit of work.
1. Create a folder @char and add it to the MATLAB path
2. Inside @char add the following two functions (with appropriate names)
function disp(X)
if ischar(X) && ~isempty(getappdata(0, 'FormatString')) && ~getappdata(0, 'FormatString')
X = [sprintf('%c \b', X), sprintf('\n')];
end
builtin('disp', X);
end
function display(X)
if ischar(X) && ~isempty(getappdata(0, 'FormatString')) && ~getappdata(0, 'FormatString')
X = [sprintf('%c \b', X), sprintf('\n')];
end
eval([inputname(1), ' = X;']);
eval(['builtin(''display'', ', inputname(1), ');']);
end
You can then toggle the formating as follows:
>> setappdata(0, 'FormatString', []);
>> str = '<test TEST>'
TEST
>> setappdata(0, 'FormatString', true);
>> str = '<test TEST>'
TEST
>> setappdata(0, 'FormatString', false);
>> str = '<test TEST>'
<test TEST>
  4 Comments
Daniel Shub
Daniel Shub on 29 Aug 2012
@Jonathan, 1) can you share your EVAL free solution, 2) please ask the hover question as a new question, 3) please consider voting for some of the answers that you like.
Jonathan
Jonathan on 6 Sep 2012
1)
function display(varargin)
if isequal(get(0, 'FormatSpacing'), 'compact')
disp([inputname(1) ' =']);
disp(varargin{1});
else
disp(' ');
disp([inputname(1) ' =']);
disp(' ');
disp(varargin{1});
end
end
This calls disp without duplicating its code. It's not as fast as the built-in display, but faster than using eval - especially using it twice. I considered not having a display implementation at all. The problem is the built-in display function calls the built-in disp function without checking for an override.
2) I am disinclined to ask the hover question separately. No variation on this page is a general solution. Solving the hover problem would still not be a general solution. Here are some related thoughts for those who desire more functionality.
If I'm working from the command line only, I can call s = dbstack();, then include isempty(s) in the if check - both inside the disp code above. This gives command line text-without-formatting without having a hover problem. This is not a general solution to the hover problem, though.
The @char folder approach does not affect text formatting when displaying a string in a struct or cell array. This is still a problem to be handled for any general solution.
Replacing the disp.m file in the Matlab toolbox with the code above does not affect the code that is executed on calling disp(). I mention this for those who are curious, not as a suggested avenue of investigation.
3) Thank you for reminding me of this option.

Sign in to comment.


Thomas
Thomas on 1 Mar 2012
The only way I could think of is:
fprintf('< \b');fprintf('a href="test">TEST</a>')
Outputis:
<test TEST>
  1 Comment
Jonathan
Jonathan on 1 Mar 2012
I see how this produces the result for this small example. The larger problem is that I have multiple hyperlinks stored in 'str'. I want to see all of the unformatted content of 'str' all at once.

Sign in to comment.

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!