如何正确获得Matlab数据点标签?第2部分 [英] How do I get the Matlab data point labels correct? Part 2
问题描述
我已经创建了一些Matlab代码,任何人都可以运行并查看问题.
I've created some Matlab code which anyone helping can run and see the problem.
当我运行以下代码时,对于我的绘图上的每个数据点,我似乎都获得了全部15个标签,而不是只有1个特定标签.
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.
那么我如何使Matlab数据点标签对于以下代码正确?
So how do I get the Matlab data point labels correct for the following code?
Based on the the suggestions from my previous question, I did the following:
我替换了这两行代码:
labels = num2str(test_vector_label,'F%d');
labels_cell = cellstr(labels);
建议使用以下代码行:
labels_cell = strread(num2str(test_vector_label),'%s');
现在有两个后续问题:
1)出现警告,指出我应该使用textscan
而不是strread
:
1) A warning appears stating that I should use textscan
instead of strread
:
labels_cell = textscan(num2str(test_vector_label),'%s');
然后,当我在上述代码的上一行中使用textscan
时,出现错误了吗?
Then when I use textscan
as in the above line of code above, I get an error?
"使用文本的错误字符串的单元格数组只能包含字符串和 数字矩阵"
"Error using text Cell array of strings may only contain string and numeric matrices"
"Code_Test中的错误(第46行) 文字(x_val,y_val,labels_cell,'水平','左侧', 'vertical','bottom')"
"Error in Code_Test (line 46) text(x_val,y_val,labels_cell,'horizontal','left', 'vertical','bottom')"
2)如何在数字标签前放置字母?例如,在原始代码中,我在字母F后面加上数字?
2) How do I put a letter in front of the number labels? For example, in the original code I had put letter F followed by a number?
%--------------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 返回一个单元格数组,而不是一个字符串数组.尝试以下
textscan returns a cell array and not an array of strings. Try the following
labels_cell = textscan(num2str(test_vector_label),'%s');
labels_cell = strcat('F',labels_cell{1});
这篇关于如何正确获得Matlab数据点标签?第2部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!