[英]How do I get the Matlab data point labels correct?
我創建了一些Matlab代碼,任何人都可以運行並看到問題的代碼。
當我運行以下代碼時,對於繪圖上的每個數據點,我似乎都獲得了全部15個標簽,而不是只有1個特定標簽。
那么,如何使Matlab數據點標簽對以下代碼正確?
根據建議,我執行了以下操作:
我替換了這兩行代碼:
標簽= num2str(test_vector_label,'F%d');
labels_cell = cellstr(標簽);
按照建議的這一行代碼:
labels_cell = strread(num2str(test_vector_label),'%s');
現在有兩個后續問題:
1)出現警告,指出我應該使用textscan而不是strread:
labels_cell = textscan(num2str(test_vector_label),'%s');
然后,當我在上面的代碼行中使用textscan時,出現錯誤嗎?
“使用文本單元格字符串數組時出錯,只能包含字符串和數字矩陣”
“ Code_Test(第46行)文本(x_val,y_val,labels_cell,“水平”,“左”,“垂直”,“底部”)中的錯誤”
2)如何在數字標簽前放置字母? 例如,在原始代碼中,我在字母F后面加上數字?
%--------------Randomly select training and testing data.-----------
num_data = 35;
data_idx = 1:35;
train_data_idx_tmp = randsample(num_data,20)
train_dataRand_idx = sort(train_data_idx_tmp)
% Lia = ismember(A,B) returns an array the same size as A, containing 1 (true)
% where the elements of A are found in B, and 0 (false) elsewhere.
test_data_idx_tmp = ismember(data_idx,train_dataRand_idx)
test_dataRand_idx = data_idx(~test_data_idx_tmp)'
% Check to see if training and test data index are exclusive.
check_train_test_idx = ismember(train_dataRand_idx,test_dataRand_idx)
%--------------------------------------------------------------------------
% Testing stage.
test_vector = test_dataRand_idx; %Select randomly obtained testing data.
% Training stage.
train_vector = train_dataRand_idx; %Select randomly obtained training
x_val = [1:15];
y_val = 2*[1:15];
plot(x_val,y_val,'or','MarkerFaceColor','r')
grid on
%Put specific data point labels on plots.
test_vector_label = test_vector';
labels = num2str(test_vector_label,'F%d');
labels_cell = cellstr(labels);
text(x_val,y_val,labels_cell,'horizontal','left', 'vertical','bottom')
您的變量labels_cell
是1x1的字符串單元格,而不是字符串數組。 更換
labels = num2str(test_vector_label,'F%d');
labels_cell = cellstr(labels);
與
labels_cell = strread(num2str(test_vector_label),'%s');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.