How do I get the Matlab data point labels correct? Part 2

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

I've created some Matlab code which anyone helping can run and see the problem.

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.

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:

I replaced these two lines of code:

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

With this line of code as suggested:

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:

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?

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

回答1:

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});


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!