Build charts from Excel.
Ältere Kommentare anzeigen
Hi everyone, I have a problem. Basically I have to work on an Excel sheet with many values. I have to create 8 charts with 8 different data sets inside. The ranges that I have to use are equally spaced in every series of data and in every scatter plot that I have to create so I had thought of using a code with for loops that would allow me to automate this procedure. But I can't build it. Initially I thought of creating a macro on VBA. I attach a txt (test) with the macro that I created on VBA which however does not work. Each series of data is made up of 7 values (range from 2 to 9 then from 10 to 17 and so on up to 58-65) the one relating to the attachment is only the first of the 8 graphics that I have to build. The second scatter plot will consist of the same data series number as the first with the same names only that the ranges will be out of phase by 64 for example if in the first chart the ranges of the first data series is 2-9 in the second chart it will be 66 -73, the second series 74-81 and so on for the other series of this second graph up to 122-129. This procedure, as I said, must be automated for 8 different graphs each consisting of 8 data series, the data series of each graph have a step of 7 and from one graph to another of 64 up to the last series of the eighth chart whose range will be 506-513. I hope I was clear, for a person who has practice in these things I don't think it's something very complicated but unfortunately I can't. If someone could help me I would be infinitely grateful because it would greatly facilitate the work I have to do. Thank you.
I also attach the excel sheet on which I have to work.
The macro that I attack have many problems:
1. I have to enter the names of the data series by hand and this is very inconvenient since the series in all the graphs are called the same way. The series names are always in order: 2.5 kW, 5 kW, 7.5 kW, 10 kW, 12.5 kW, 15 kW, 17.5 kW, 20 kW.
2. Create an initial curve in the extra chart which calls "series1"
3. It doesn't always work, sometimes it creates as many data series as the columns of the excel file that I have attached previously.
But the index update criteria are correct
3 Kommentare
Image Analyst
am 5 Jan. 2020
Can you mock up and attach a screenshot of what you'd like to obtain?
Rocco De Luca
am 5 Jan. 2020
Rocco De Luca
am 5 Jan. 2020
Antworten (1)
Mohammad Sami
am 6 Jan. 2020
You can use two for loops.
The outer for loop can iterate over the column C, and subset the data and create new figure.
The inner for loop can interate over column D in the subset of the data to plot line.
t = readtable('ScenarioA4-20km.xlsx');
uniq_col_c = unique(t.Pw_kW_);
for i = 1:length(uniq_col_c)
Pw_kW = uniq_col_c(i);
t_sub = t(t.Pw_kW_ == Pw_kW,:); % subset the data by C
figure; %create new figure
hold on;
uniq_col_d = unique(t_sub.Ppv_kW_);
for j = 1:length(uniq_col_d)
% plot lines for each Ppv_kW
Ppv_kW = uniq_col_d(j);
t_sub_sub = t_sub(t_sub.Ppv_kW_ == Ppv_kW,:); % subset the data again by D
plot(t_sub_sub.Pb_kWh_,t_sub_sub.PowerToBa,'DisplayName',num2str(Ppv_kW)); % plot e vs q
end
hold off;
end
4 Kommentare
Rocco De Luca
am 6 Jan. 2020
Mohammad Sami
am 7 Jan. 2020
The readtable function will import the data as a table and use the names in first row to create the header.
You can select which variable to plot by changing the script to another variable name in your table. (table.varname)
t = readtable('ScenarioA4-20km.xlsx');
uniq_col_c = unique(t.Pw_kW_);
for i = 1:length(uniq_col_c)
Pw_kW = uniq_col_c(i);
t_sub = t(t.Pw_kW_ == Pw_kW,:); % subset the data by C
figure; %create new figure
hold on;
uniq_col_d = unique(t_sub.Ppv_kW_);
for j = 1:length(uniq_col_d)
% plot lines for each Ppv_kW
Ppv_kW = uniq_col_d(j);
t_sub_sub = t_sub(t_sub.Ppv_kW_ == Ppv_kW,:); % subset the data again by D
% the following plot Pb_kWh_ against PowerToBa
% you can change to other variable names to plot something else
plot(t_sub_sub.Pb_kWh_,t_sub_sub.PowerToBa,'DisplayName',num2str(Ppv_kW)); % plot e vs q
end
hold off;
end
To select the sheet, you can run readtable with import options
opts = detectImportOptions('ScenarioA4-20km.xlsx','Sheet','Medio'); % the sheet to import
% you can also set many other options, check the documentation
t = readtable('ScenarioA4-20km.xlsx',opts);
Rocco De Luca
am 7 Jan. 2020
Mohammad Sami
am 8 Jan. 2020
detectImportOptions was introduced in R2016b. Hence the error in R2016a.
For R2016a, you can directly specify the sheet as a name value pair in readtable function
'Sheet' — Worksheet to read1 (default) | positive integer indicating worksheet index | string containing worksheet name
Worksheet to read, specified as the comma-separated pair consisting of 'Sheet' and a positive integer indicating the worksheet index or a string containing the worksheet name. The worksheet name string cannot contain a colon (:). To determine the names of sheets in a spreadsheet file, use [status,sheets] = xlsfinfo(filename). For more information, see xlsfinfo.
Example: 'Sheet', 2
Kategorien
Mehr zu Spreadsheets finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
