Display Text Without Formatting
조회 수: 29 (최근 30일)
이전 댓글 표시
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
Daniel Shub
2012년 3월 1일
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.
채택된 답변
Andrew Newell
2012년 3월 1일
You can replace all the occurrences of href by HREF for display:
str = {'<test TEST1>';'<test TEST2>'};
dispStr = regexprep(str,'href','HREF');
disp(dispStr)
추가 답변 (4개)
Daniel Shub
2012년 3월 1일
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
Daniel Shub
2012년 8월 29일
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.
Daniel Shub
2012년 8월 29일
편집: Daniel Shub
2012년 8월 29일
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
Daniel Shub
2012년 8월 29일
@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.
Thomas
2012년 3월 1일
The only way I could think of is:
fprintf('< \b');fprintf('a href="test">TEST</a>')
Outputis:
<test TEST>
참고 항목
카테고리
Help Center 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!