How to display a list from excel using different values
Show older comments
I have an excel spreadsheet with 3 columns of information that I am using: A is a list of country names, B is a % value, and C is a total value. In matlab I imported the information, assigned each column a variable name, and created a table out of it. I then created a new column to the table within matlab by multiplying the values of B by C. Finally I used if/elseif statements to break up the values of this new column to separate them into 4 value ranges (NaN, 0-299, 300-2499, and 2500+)
My question is, how can I have matlab display the certain country names from column A, associated with the specific range of values from the newest column (from highest value to lowest)?
2 Comments
Scott MacKenzie
on 24 Apr 2021
Edited: Scott MacKenzie
on 24 Apr 2021
Why don't you post your spreadsheet, or a reasonable subset of it. I can picture the solution, but I'm not inclined to create a mock data set to work through the details.
Accepted Answer
More Answers (1)
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/595985/New_Percent_Land_Degraded_(Revised%20Country%20List_59).xlsx';
opt = detectImportOptions(filename, 'preservevariablenames', true);
opt = setvaropts(opt, 3, 'type', 'double');
T = readtable(filename, opt);
T.Usable = T{:,3} .* T{:,4};
[counts, edges, bins] = histcounts(T.Usable, 4);
for quarter = 1 : 4
fprintf('\nQuartile %d: %g - %g\n', quarter, edges(quarter:quarter+1));
disp(categorical(T.GeoAreaName(bins == quarter)));
end
1 Comment
Walter Roberson
on 25 Apr 2021
Notice the fundamental mechanic here: determine which "bin" applies each data item, using histcounts (or using discretize). Then you can compare the bin number against a particular bin number to create a logical mask that matches only the entries that had that range of values. You can use that mask to extract the appropriate subset of values, such as T.Usable(bins == quarter) and you can extract the country names using T.GeoAreaName(bins == quarter). Do you know how to sort two items with respect to each other? See the second output of sort()
Categories
Find more on Spreadsheets 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!