如何让 Matlab 数据点标签正确?第2部分
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
时,我得到一个错误?
"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')"
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 returns 元胞数组而不是字符串数组。尝试以下
labels_cell = textscan(num2str(test_vector_label),'%s');
labels_cell = strcat('F',labels_cell{1});
我创建了一些 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
时,我得到一个错误?
"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')"
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 returns 元胞数组而不是字符串数组。尝试以下
labels_cell = textscan(num2str(test_vector_label),'%s');
labels_cell = strcat('F',labels_cell{1});