[英]How do I get the Matlab data point labels correct?
I've created some Matlab code which anyone helping can run and see the problem. 我创建了一些Matlab代码,任何人都可以运行并看到问题的代码。
When I run the following code, for each data point on my plot I seem to get all 15 labels instead of only 1 specific label. 当我运行以下代码时,对于绘图上的每个数据点,我似乎都获得了全部15个标签,而不是只有1个特定标签。
So how do I get the Matlab data point labels correct for the following code? 那么,如何使Matlab数据点标签对以下代码正确?
Based on the the suggestions, I did the following: 根据建议,我执行了以下操作:
I replaced these two lines of code: 我替换了这两行代码:
labels = num2str(test_vector_label,'F%d'); 标签= num2str(test_vector_label,'F%d');
labels_cell = cellstr(labels); labels_cell = cellstr(标签);
With this line of code as suggested: 按照建议的这一行代码:
labels_cell = strread(num2str(test_vector_label),'%s'); labels_cell = strread(num2str(test_vector_label),'%s');
Now there are two follow-up questions: 现在有两个后续问题:
1) A warning appears stating that I should use textscan instead of strread: 1)出现警告,指出我应该使用textscan而不是strread:
labels_cell = textscan(num2str(test_vector_label),'%s'); labels_cell = textscan(num2str(test_vector_label),'%s');
Then when I use textscan as in the above line of code above, I get an error? 然后,当我在上面的代码行中使用textscan时,出现错误吗?
"Error using text Cell array of strings may only contain string and numeric matrices" “使用文本单元格字符串数组时出错,只能包含字符串和数字矩阵”
"Error in Code_Test (line 46) text(x_val,y_val,labels_cell,'horizontal','left', 'vertical','bottom')" “ Code_Test(第46行)文本(x_val,y_val,labels_cell,“水平”,“左”,“垂直”,“底部”)中的错误”
2) How do I put a letter in front of the number labels? 2)如何在数字标签前放置字母? For example, in the original code I had put letter F followed by a number?
例如,在原始代码中,我在字母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')
Your variable labels_cell
is a 1x1 string cell not an array of strings. 您的变量
labels_cell
是1x1的字符串单元格,而不是字符串数组。 Replace 更换
labels = num2str(test_vector_label,'F%d');
labels_cell = cellstr(labels);
with 与
labels_cell = strread(num2str(test_vector_label),'%s');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.