1
我有一個抽象類Writer
,它允許客戶寫入。可能是屏幕,可能是一個文件。現在,我嘗試創建派生類來寫入字符串。在Matlab中正確寫入字符串(高效並保留轉義字符)
我有兩個問題與表示爲線方法write(...)
:
- 這也可能是非常低效的。有沒有像Matlab中的字符串緩衝區?
- 它將轉義序列(如
\n
)寫入字符串中,而不是採用其實際含義。
我該如何正確地得到表示線?
代碼:
classdef StringTextWriter < Writer
properties
str;
end
methods
function this = StringTextWriter()
% Init the write-target which is a string in our case.
% (Other Writer classes would maybe open a file.)
this.str = '';
end
function write(this, val)
% Write to writer target.
% (Other Writer classes would maybe use fprinf here for file write.)
% ?????????????????????????????
this.str = [this.str val]; % How to do this properly?
% ?????????????????????????????
end
end
end