How can i organize the images in my code?

5 views (last 30 days)
Hi, I just wrote a code for the recognition of handwritten digits but I have encountered one problem. In my code i want to show images and diagrams right below the corrispondent section of the code. For instance i decided to use a for loop in order to show all the different ten digits, but when i run the code the images go to the end of the whole code, how can i solve this problem? If it can help I'll share the code, let me know if the same thing happen to you as well. The dataset can be found here http://archive.ics.uci.edu/ml/datasets/Optical+Recognition+of+Handwritten+Digits
Y_tra = load('optdigits.tra'); %carico il training set
Y_tes = load('optdigits.tes'); %carico il test set
features = 1:64;
Nfeatures = length(features);
N = size(Y_tra,1);
X = Y_tra(:,features);
Y_class = Y_tra(:,65); %indica le classi nel training set
class_labels = categorical(unique(Y_class)); %vediamo quante classi ci sono e le trasformiamo in dato categorical
Ncl = length(class_labels);
Y_class_ones = zeros(Ncl,N); %per usare alcune funzioni bisogna esprimere i dati come matrici, con 1 in corrispondenza della classe (righe) per ogni item (colonne)
for c=1:Ncl
Y_class_ones(c,find(Y_class==c-1))=1;
end
figure %utilizzo un ciclo for nel test set per mostrare le 10 cifre
for i=1:10
subplot(2,5,i)
imagesc(reshape(Y_tes(i,1:64),8,8)');
colormap("gray");
axis square;
caption = sprintf('#%d', i-1);
title(caption);
end
trainRatio = 0.5; %metà per il training effettivo
valRatio=0.25; %un quarto per validation
testRatio=0.25; %un quarto per il writer depending testing, ossia il test set
[trainInd,valInd,testInd] = dividerand(N,trainRatio,valRatio,testRatio);
Ytra = Y_tra(trainInd,:); %traing set
Ytra_class = Y_class(trainInd);
Ntr = size(Ytra,1);
Yts = Y_tra(testInd,:); %test set
Yts_class = Y_class(testInd);
Nts = size(Yts,1);
Ytr_class_ones = Y_class_ones(:,trainInd);
Yts_class_ones = Y_class_ones(:,testInd);
[U,Xpc,S] = pca(Ytra);
figure('Position', [0 0 600 600]);
line(Xpc(:,1),Xpc(:,2),Xpc(:,3), 'linestyle', 'none', 'marker', '.');
xlabel('PC 1');
ylabel('PC 2');
zlabel('PC 3');
box off;
view(3);
hold on
biplot(400*U(:,1:3));
D = size(Ytra,2);
figure('position', [0 0 600 200]);
bar(1:D,100*S./sum(S));
xlabel('# components');
ylabel('% var');
title('Pareto Diagram');
box off
classtype = "naivebayes"
nt = 1;
px_C = [];
PrC_x=[];
auc = zeros(length(classtype),Ncl);
for type = classtype
switch type
case 'naivebayes'
%Train
Mdl = fitcnb(Ytra,Ytra_class,'DistributionNames',"mn");
%test
[label,Posterior] = predict(Mdl,Yts);
Yts_pred = Posterior';
case 'mln'
Nhid = 5;
%train
net = patternnet(Nhid);
net_tr = train(net, Ytra', Ytr_class_ones);
net_ts = train(net, Yts', Yts_class_ones);
view(net_tr);
%test
Yts_pred = net_tr(Yts');
Ytr_pred = net_tr(Ytra');
end
plotconfusion(Yts_class_ones, Yts_pred);
set(gcf,'Position',[0 0 500 500], "color", 'w'); %test set
title(type);
%plotconfusion(Ytr_class_ones, Ytr_pred);
%set(gcf,'Position',[0 0 500 500], "color", 'w'); %training set
%title(type);
end

Answers (1)

Sourabh Kondapaka
Sourabh Kondapaka on 8 Feb 2021
Edited: Sourabh Kondapaka on 8 Feb 2021
Looks like Matlab Live Script can help with what you want to achieve. Please check the following Documentation link
You can check this video which explains what a Matlab Live Editor is.
You can also check the following video on YouTube.
  2 Comments
Fabrizio Lepori
Fabrizio Lepori on 11 Feb 2021
Hi, thanks for your answer, I am already using a Live Script, but when i run my code all the images go to the end of the code.
Sourabh Kondapaka
Sourabh Kondapaka on 17 Feb 2021
Is it because all of the above code is in 1 section ?
Check for the "Section" group, click on "Section Break".
You need to move the code where you want to display images into a seperate section so that, that particular section has its own output.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!