2016-06-14 121 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);'(參見編輯) –