Bar chart from Excel with hidden columns
106 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Florian
le 7 Août 2024 à 17:09
Déplacé(e) : Stephen23
le 9 Août 2024 à 5:07
Hi there
I want to create the attached chart (plus an average line per section) with Matlab. I can do it super quick with Excel, but unfortunately it didn´t work well with Matlab.
There are three complications for me. 1 - How can I can exclude the hidden columns? 2 - What is the best way to deal with German numbers with comma instead of point? 3 -And Is there any way to have an average line per section?
Seems the "readtable" doesn´t work well with commas.
Thanks
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1748729/image.png)
7 commentaires
Stephen23
le 8 Août 2024 à 17:18
Déplacé(e) : Stephen23
le 9 Août 2024 à 5:07
"I raise it to Q once I know the error isn´t me haha"
In fact the data in column C is actually text. You can check this yourself using the TYPE function (in german TYP):
You can also check the type by viewing the Open Office XML file content. The Open Office standard specifies that the t="s" attribute&value defines that cell content as being text. Lets take a look at cell C2:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1749724/image.png)
Yep, C2 is definitely marked as being text content. As are all other values in column C.
(As an aside, the value 23 refers to the sharedString where that text content is actually defined)
Here is a third approach to check the content of column C. Lets import the data using READCELL:
C = readcell('Auswertung - Kopie.xlsx')
So I used three different independent methods to confirm the type of the data in column C. All of them told me that the content of column C is text. Which means that READTABLE is correctly importing text as text. Which is the expected, documented, desired behavior of READTABLE. Which means that the problem lies with the file data, not with READTABLE.
The best place to fix this import is to fix the incorrectly defined file data. First change the cell format to "general" and then use the approach given under "Convert a column of text to numbers" here:
Réponse acceptée
Voss
le 8 Août 2024 à 17:37
Modifié(e) : Voss
le 8 Août 2024 à 22:18
filename = 'Auswertung - Kopie.xlsx';
opts = detectImportOptions(filename);
opts = setvartype(opts,2:numel(opts.VariableTypes),'double');
opts.VariableNamingRule = 'preserve';
T = readtable(filename,opts);
cats = T{:,1};
% data = T{:,2:end};
data = T{:,[4 6:8 10:end]};
idx = all(isnan(data),2);
cats(idx) = [];
data(idx,:) = [];
hb = bar(data.','FaceColor','flat');
colors = [0 0.4 0.7; 0.5 0.5 0.5; 1 0.5 0; 0.6 0 0; 0 0.6 0];
set(hb,{'CData'},num2cell(colors,2));
mean_data = mean(data,1,'omitnan');
N = size(data,2);
yd = [mean_data([1 1],:); NaN(1,N)];
xd = 0.5*[-1; 1; NaN]+(1:N);
hl = line(xd(:),yd(:),'Color','k','LineStyle',':','LineWidth',2);
legend([hb hl],[cats; {'Averages'}],'Location','best')
set(gca(), ...
'YScale','log', ...
'XLim',[0 N+1], ...
'XTick',1:N, ...
...'XTickLabels',T.Properties.VariableNames(2:end))
'XTickLabels',T.Properties.VariableNames([4 6:8 10:end]))
0 commentaires
Plus de réponses (2)
dpb
le 8 Août 2024 à 17:37
Modifié(e) : dpb
le 8 Août 2024 à 18:48
warning('off','MATLAB:table:ModifiedAndSavedVarNames'); % turn off annoying nag message
tA=readtable('Auswertung - Kopie.xlsx','MissingRule','omitrow'); % read, don't bring missing rows
tA.Cell=categorical(tA.Cell); % convert types
tA.Diameter=str2double(tA.Diameter); % ditto, w/o the opt object
head(tA) % look at what's we gots...
ixBar=[4 6 8:width(tA)]; % variables wanted in bar()
x=(tA.Properties.VariableNames(ixBar)); % create an x vector for labelling
hB=bar(x,tA{:,ixBar}.','Grouped'); % pull the columns; transpose for grouping
hL=legend(tA.Cell,'location','northeast'); % label 'em...
ylim([0 1])
Not terribly interesting given all the data are the same by color (whatever that represents), but the idea should be clear.
Left as "exercise for Student" to add a row to the table that is the mean for each... :)
0 commentaires
dpb
le 7 Août 2024 à 18:13
Modifié(e) : dpb
le 7 Août 2024 à 19:30
opt=detectImportOptions('yourfile.xlsx'); % base import options object
opt.SelectedVariableNames=opt.SelectedVariableNames([1 3:end]); % don't read the second column
opt=setvaropts(opt,'DecimalSeparator',','); % set comma as decimal
tT=readtable('yourfile.xlsx',opt);
MATLAB may need a little additional help given it doesn't have all of Excel built in...thank goodnes!!! :)
Above would be a start; you may want to add some additional details and it's certainly easy enough to then compute means or whatever statistics desired...the extra lines may be something else to deal with; not sure what, if anything, might be behind the graphic; if there are just extra labels in the first column for "in case", then the 'MissingRule' may be useful to not read those in...
Voir également
Catégories
En savoir plus sur Data Import from MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!