[英]How do I get the Matlab data point labels correct? Part 2
我创建了一些Matlab代码,任何人都可以运行并看到问题的代码。
当我运行以下代码时,对于绘图上的每个数据点,我似乎都获得了全部15个标签,而不是只有1个特定标签。
那么,如何使Matlab数据点标签对以下代码正确?
根据上一个问题的建议,我进行了以下操作:
我替换了这两行代码:
labels = num2str(test_vector_label,'F%d');
labels_cell = cellstr(labels);
按照建议的这一行代码:
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')
textscan返回单元格数组而不是字符串数组。 尝试以下
labels_cell = textscan(num2str(test_vector_label),'%s');
labels_cell = strcat('F',labels_cell{1});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.