How to display a list from excel using different values
Mostrar comentarios más antiguos
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 comentarios
Scott MacKenzie
el 24 de Abr. de 2021
Editada: Scott MacKenzie
el 24 de Abr. de 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.
Respuesta aceptada
Más respuestas (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 comentario
Walter Roberson
el 25 de Abr. de 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()
Categorías
Más información sobre Spreadsheets en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!