2016-06-14 233 views
1

我发现了一些解决方案,但没有使用Numberarray的单元。
问题很简单,我有一个Array a=(0,1,2,3,4,5,6,7)我想改变每个其他值与“空白”像这样a=(0,'',2,''...),使数组保持相同的长度,但只有其他每个值。当我尝试像这样a(2:2:end)='';我得到a=(0,2,4,6)的长度是不一样的。
当我尝试a(2:2:end)=blanks(1);它几乎工作:),但不完全,我得到a=(0,'32',2,'32',4,'32'...)我知道,实际上32意味着'空间'(ASCII)什么实际上意味着它正常工作。然后我尝试使用它来设置我的TickLabels,但它将它解释为32不像ASCII。在Matlab中用空白替换值

+0

你的目标究竟是什么?即为什么你不能使用单元阵列,如果它适用于设置XTick标签? –

回答

2

您不能在数组中引入空格作为条目。你只能引入数字。

如果您希望使用它作为刻度标记,转换为一个单元阵列,然后你可以设置一些细胞的内容[](空):

a = [0 1 2 3 4 5 6 7]; % original vector 
a = num2cell(a); % convert to cell 
a(2:2:end) = {[]}; % set some cells' contents to [] 

x = 1:8; % x data for example plot 
y = x.^2; % y data for example plot 
plot(x, y) % x plot the graph 
set(gca, 'xticklabels', a) % set x tick labels 

enter image description here

要获得剔没有科学记数法的标签使用num2str并使用适当的格式:

a = [0 1 2 3 4 5 6 7]*1e6; % original vector 
a = num2cell(a); % convert to cell 
a(2:2:end) = {[]}; % set some cells' contents to [] 
a = cellfun(@num2str, a, 'Uniformoutput', false); % convert each number to a string 

x = [0 1 2 3 4 5 6 7]*1e6; % x data for example plot 
y = x.^2; % y data for example plot 
plot(x, y) % x plot the graph 
set(gca, 'xticklabels', a) % set x tick labels 
+0

是的,我现在明白了,当我读到TickLabel时,它是一个Cell,但是Ticks themselvs是我现在看到它的Double Arrays。非常感谢您的支持。小问题,我读了Ticks: a = ax1.XTick; %a [100000,200000,300000,400000,...,1000000] a = num2cell(a); a(2:2:end)= {[]}; ax1.XTickLabel = a; 然后我得到的情节几乎是正确的1 000 000显示为1e + 06,我不明白,我认为细胞是字符串不是数字:)。 – GDD

+0

我试过这种方法ax1.TickLabelInterpreter ='none'; 但仍然1e + 06。 – GDD

+0

@GDD试试'a = cellfun(@ num2str,a,'Uniformoutput',false);'(参见编辑) –