Matlab issues with plotting a variable
2 views (last 30 days)
Show older comments
Joseph Chudnovsky
on 17 May 2021
Commented: Star Strider
on 17 May 2021
Hello,
I have a csv file that I would like to take 2 columns from and make them x and y axis. I managed to plot one of the columns however the other column always gives me an error whenever I try to plot with it. I attached the data file below. Here is my code:
intel_cpus=readtable('intel.csv');
C=intel_cpus.("Cores");
E = intel_cpus.("BaseSpeed");
plot(C,'.')
I am trying to get columns D and F as axis of my graph. Thank you for any help!
1 Comment
Mathieu NOE
on 17 May 2021
hello
I noticed that the csv file as not same number of separators in all lines (separator = comma)
see the result in the table will right shift some fields
this is also obvious when you convert inot xlsx file with comma separator
need some cleaning first ...
Accepted Answer
Star Strider
on 17 May 2021
There are gaps in the ‘Base Speed’ variable, so these need to be detected and the corresponding rows of ‘Cores’ eliminated in order for the vectors to have the same number of elements, as required by plot.
intel_cpus = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/620443/intel%20(2).csv','VariableNamingRule','preserve')
% Q2 = intel_cpus(178:198,:)
C = intel_cpus.('Cores');
E = intel_cpus.('Base Speed');
Lc = cellfun(@(x)~isempty(x), E, 'Unif',0); % Cell Array Of Logical Values
Lv = [Lc{:}]'; % Logical Vector
En = regexp(E,'\d+.\d+','match'); % Get Numeric Data
En = str2double([En{:}]).'; % Extract Numeric Data
figure
plot(C(Lv),En,'.')
grid
xlabel('Cores')
ylabel('Base Speed')
.
7 Comments
Star Strider
on 17 May 2021
The easiest way to do that would be to use regexp to isolate the core designations, then use some sort of logical comparison or findgroups to get the cores you want, for example —
intel_cpus = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/620443/intel%20(2).csv','VariableNamingRule','preserve')
% Q2 = intel_cpus(178:198,:)
Pc = regexp(intel_cpus.Processor, 'i\d', 'match');
Pn = [Pc{:}]'
idx_i9 = ismember(Pn, 'i9')
% C = intel_cpus.('Cores');
% E = intel_cpus.('Base Speed');
% Lc = cellfun(@(x)~isempty(x), E, 'Unif',0); % Cell Array Of Logical Values
% Lv = [Lc{:}]'; % Logical Vector
%
% En = regexp(E,'\d+.\d+','match'); % Get Numeric Data
% En = str2double([En{:}]).'; % Extract Numeric Data
%
% figure
% plot(C(Lv),En,'.')
% grid
% xlabel('Cores')
% ylabel('Base Speed')
Then use that logical vector (‘idx_i9’ here) similarly to the way I used ‘Lv’ in the plot call, to define the rows of interest. Note that this would likely require combining logical vectors using the and,& function in order that the selected rows or vectors reflect both conditions (and any other logical conditions that might arise from further processing).
.
More Answers (1)
Markus
on 17 May 2021
Edited: Markus
on 17 May 2021
I think you want to remove the characters after the floating values. You cannot plot the values with the units attached.
While it might not be the cleanest, you could just go for:
D = str2double(replace(intel_cpus.("MaxTurboSpeed"),{' GHz',' MHz'},''));
and
E = str2double(replace(intel_cpus.("BaseSpeed"),{' GHz',' MHz'},''));
F = str2double(replace(intel_cpus.("CacheSize"),{' MB'},''));
Please do remember to add some code to keep the unit info regarding giga and mega.
I assume you are using this for something like an overview, so it should be fine.
0 Comments
See Also
Categories
Find more on Bar Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!