[英]Matlab: How can formatSpec work with '?
I want to write a cell array in a (m-)file in a way that it is interpreted as a cell: cell elements quoted 'element' and separated with comma and blank. 我想以(m-)文件的形式写一个单元格数组,将其解释为一个单元格:单元格元素用'element'引用,并用逗号和空格分隔。 To archive this formatSpec should handle ' which it can not directly: formatSpec = ''%s', ' is not working (same as strrep(S, '§', ''')).
要存档此formatSpec,应处理不能直接处理的'formatSpec ='%s','不起作用(与strrep(S,'§',''')相同)。 Is there a way to mark ' like with \\ in regular expressions?
有没有办法在正则表达式中用\\标记“喜欢”? Here a code example where ' is substituted with §:
这是一个用'代替§的代码示例:
Pop_plus = {'(-1./z)', '(1./(z.^-2))', '((z-1)./(z+1))', '(((z+1)./z)./2)', '(z+sqrt((z.^2)-1))', '(1./(4.*(z.^2)-1))'};
Pop_plus_out_path_m = ...\Pop_plus.m;
fileID = fopen(Pop_plus_out_path_m, 'wt');
formatSpec = '%s';
L1 = {'F = {'};
fprintf(fileID,formatSpec,L1{1});
formatSpec = '§%s§, ';
for i = 1:numel(Pop_plus)-1
fprintf(fileID,formatSpec,Pop_plus{i});
end
formatSpec = '§%s§';
fprintf(fileID,formatSpec,Pop_plus{numel(Pop_plus)});
L2 = {'};'};
formatSpec = '%s';
fprintf(fileID,formatSpec,L2{1});
fclose(fileID);
result: F = {§(-1./z)§, §(1./(z.^-2))§, §((z-1)./(z+1))§, §(((z+1)./z)./2)§, §(z+sqrt((z.^2)-1))§, §(1./(4.*(z.^2)-1))§};
This isn't just an issue when using it as a format specifier, this is an issue whenever you are attempting to define a character array. 将其用作格式说明符时,这不仅是一个问题,而且当您尝试定义字符数组时,这也是一个问题。 You need to escape
'
with another '
when trying to use it within a character array. 尝试在字符数组中使用时,需要用另一个
'
转义'
。
'''string'
% 'string
For your specific example, you could create your format specification 对于您的特定示例,您可以创建格式规范
formatSpec = '''%s''';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.