Hi,
I have this kind of data which is the position of each users (attached) but the problem when I plot them it's getting miss. Any suggestions to plot this data?
clear;
clf('reset');
max_iterations = 500;
xs = 1: max_iterations;
num_devices=50;
N=importdata('40_3_1.txt');
r=mean(reshape(N,2,[]));
r = r';
ys_00 =r(1:max_iterations, 1);
b = bar(xs, ys_00);
plot(xs, ys_00);
ylim([0 num_devices]);
xlabel("Iterations");
ylabel("Number of selected clients");
legend({'B', ...
}, ...
'location', 'northwest');
set(gca,'XMinorTick','on','YMinorTick','on');
grid on;
TextFontSize=20;
LegendFontSize = 15;
set(0,'DefaultAxesFontName','Times',...
'DefaultLineLineWidth',1,...
'DefaultLineMarkerSize',6);
set(gca,'FontName','Times New Roman','FontSize',TextFontSize);
hold off;
clear;
figure;
clf('reset');

2 Kommentare

Walter Roberson
Walter Roberson am 6 Aug. 2022
What is getting missed?
Brave A
Brave A am 6 Aug. 2022
I need them as a curve all values in one line.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 6 Aug. 2022

0 Stimmen

Your data is not at all in pairs. Your data is in groups of 10 -- the data starts at 0 and returns to 0 each 10 entries.
You take the mean() over the first column, which I am guessing means that you want the mean over each group of 10.
Your legends and titles do not give me confidence that they are at all related to the input data.
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1089470/40_3_1.txt';
TextFontSize=20;
LegendFontSize = 15;
set(0,'DefaultAxesFontName','Times',...
'DefaultLineLineWidth',1,...
'DefaultLineMarkerSize',6);
set(gca,'FontName','Times New Roman','FontSize',TextFontSize);
max_iterations = 500;
xs = 1 : max_iterations;
num_devices=50;
N = readmatrix(filename, 'delimiter', '\t');
r = mean(reshape(N,10,[]), 1);
r = r';
ys_00 = r(1:max_iterations, 1);
plot(xs, ys_00);
xlabel("Iterations");
ylabel("Number of selected clients");
legend({'B', ...
}, ...
'location', 'northwest');
set(gca,'XMinorTick','on','YMinorTick','on');
grid on;

9 Kommentare

Brave A
Brave A am 6 Aug. 2022
thank you Walter.
I am uing older version of Matlab (2018). So I got this error "Undefined function or variable 'readmatrix'."
Try this -- except that you will need to change the filename to your local file, as your release did not support readtable() of an https file.
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1089470/40_3_1.txt';
TextFontSize=20;
LegendFontSize = 15;
set(0,'DefaultAxesFontName','Times',...
'DefaultLineLineWidth',1,...
'DefaultLineMarkerSize',6);
set(gca,'FontName','Times New Roman','FontSize',TextFontSize);
max_iterations = 500;
xs = 1 : max_iterations;
num_devices=50;
opt = detectImportOptions(filename, 'delimiter', '\t');
Ntab = readtable(filename, opt);
N = table2array(Ntab);
r = mean(reshape(N,10,[]), 1);
r = r';
ys_00 = r(1:max_iterations, 1);
plot(xs, ys_00);
xlabel("Iterations");
ylabel("Number of selected clients");
legend({'B', ...
}, ...
'location', 'northwest');
set(gca,'XMinorTick','on','YMinorTick','on');
grid on;
Brave A
Brave A am 6 Aug. 2022
Sorry it's still giving the error.
Error using matlab.virtualfileio.internal.stream.RemoteToLocal (line 36)
File input must be a non-empty character vector or string scalar.
Error in detectImportOptions (line 233)
remote2Local = matlab.virtualfileio.internal.stream.RemoteToLocal(filename);
Error in compare (line 146)
opt = detectImportOptions(N, 'delimiter', '\t');
Brave A
Brave A am 6 Aug. 2022
Yes I am trying to take the mean over the whole data of 1000.
Brave A
Brave A am 6 Aug. 2022
I just updated my version to 2022.
sorry again but I have this error"Error using readmatrix
"filename" must be a string scalar or character vector."
Why are you asking to detectImportOptions on N, when my code asks to detectImportOptions on filename ?? If you upgraded to R2022, then you can execute the code exactly as I posted it, without needing to change anything (including not changing the file name.)
Yes I am trying to take the mean over the whole data of 1000.
Your file has 10000 points. The 1st, 11'th, 21'st, 31'st and so on values are all 0, suggesting that your data is 1000 groups of 10 values. How many values are you expecting after the mean() ? 10 values (mean of each column)? 1000 values (mean of each group of 10) ?
You plot the means against the iteration number, and you expect more than 10 iterations, so it would not appear to make sense for you to want 10 results to be returned from the mean() call. It would seem to instead make more sense for you to set
max_iterations = 1000;
Brave A
Brave A am 6 Aug. 2022
Bearbeitet: Brave A am 6 Aug. 2022
Sorry I just updated before couple minutes to the 2022. But it's still give me in this new version error for readmatrix :( I need to run your previous code.
Error using readmatrix
"filename" must be a string scalar or character vector.
TextFontSize=20;
LegendFontSize = 15;
set(0,'DefaultAxesFontName','Times',...
'DefaultLineLineWidth',1,...
'DefaultLineMarkerSize',6);
set(gca,'FontName','Times New Roman','FontSize',TextFontSize);
max_iterations = 500;
xs = 1 : max_iterations;
num_devices=50;
N = readmatrix('40_3_1.txt', 'delimiter', '\t');
r = mean(reshape(N,10,[]), 1);
r = r';
ys_00 = r(1:max_iterations, 1);
plot(xs, ys_00);
xlabel("Iterations");
ylabel("Number of selected clients");
legend({'B', ...
}, ...
'location', 'northwest');
set(gca,'XMinorTick','on','YMinorTick','on');
grid on;
I have this right now. it's plot empty figure.:(
Tested in R2021a Pre-release, code exactly as follows.
I am currently installing R2022a for testing.
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1089470/40_3_1.txt';
TextFontSize=20;
LegendFontSize = 15;
set(0,'DefaultAxesFontName','Times',...
'DefaultLineLineWidth',1,...
'DefaultLineMarkerSize',6);
set(gca,'FontName','Times New Roman','FontSize',TextFontSize);
max_iterations = 500;
xs = 1 : max_iterations;
num_devices=50;
N = readmatrix(filename, 'delimiter', '\t');
r = mean(reshape(N,10,[]), 1);
r = r';
ys_00 = r(1:max_iterations, 1);
plot(xs, ys_00);
xlabel("Iterations");
ylabel("Number of selected clients");
legend({'B', ...
}, ...
'location', 'northwest');
set(gca,'XMinorTick','on','YMinorTick','on');
grid on;

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by