MATLAB Answers

How can i organize the images in my code?

2 views (last 30 days)
Fabrizio Lepori
Fabrizio Lepori on 4 Feb 2021
Commented: Sourabh Kondapaka on 17 Feb 2021
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
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
figure %utilizzo un ciclo for nel test set per mostrare le 10 cifre
for i=1:10
axis square;
caption = sprintf('#%d', i-1);
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;
hold on
D = size(Ytra,2);
figure('position', [0 0 600 200]);
xlabel('# components');
ylabel('% var');
title('Pareto Diagram');
box off
classtype = "naivebayes"
nt = 1;
px_C = [];
auc = zeros(length(classtype),Ncl);
for type = classtype
switch type
case 'naivebayes'
Mdl = fitcnb(Ytra,Ytra_class,'DistributionNames',"mn");
[label,Posterior] = predict(Mdl,Yts);
Yts_pred = Posterior';
case 'mln'
Nhid = 5;
net = patternnet(Nhid);
net_tr = train(net, Ytra', Ytr_class_ones);
net_ts = train(net, Yts', Yts_class_ones);
Yts_pred = net_tr(Yts');
Ytr_pred = net_tr(Ytra');
plotconfusion(Yts_class_ones, Yts_pred);
set(gcf,'Position',[0 0 500 500], "color", 'w'); %test set
%plotconfusion(Ytr_class_ones, Ytr_pred);
%set(gcf,'Position',[0 0 500 500], "color", 'w'); %training set

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.
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!

Translated by