如何正确获取Matlab数据点标签?

时间:2015-08-20 04:17:18

标签: matlab

我已经创建了一些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,'水平''左&#39 ;, '垂直''底部')"

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')

1 个答案:

答案 0 :(得分:0)

您的变量labels_cell是1x1字符串单元格而不是字符串数组。取代

labels = num2str(test_vector_label,'F%d');   
labels_cell = cellstr(labels);

labels_cell = strread(num2str(test_vector_label),'%s');