How do I sort data in a table in ascending order given specific Series type in Column

3 views (last 30 days)
Hi Everyone,
I have data that is on the year and type of different cars. I want to sort the Years in the data file in ascending order of their Series. For example, all the cars that are BMW to be sorted in ascending and then all Cadillac sorted in ascending etc. However, when i do this in a for loop, I get the following error;
Unable to perform assignment because dot indexing is not supported for variables of this type.
Error in Untitled2 (line 17)
survey.year = sortrows(survey)
I am currently using sortrows and have tried flipud but have had no sucess so any help would be great! I have attached the data file I am using.
% simulating data
% made up data on excel for cars - want to sort ascending for year
data = readtable('Cars_data.csv','VariableNamingRule','preserve');
unique(data.Series); % 15 unique
type = data.Series;
type = categorical(type);
class(type);
sp = categories(type);
for k = 1:length(sp)
m = sp(k);
G = table2cell(data(strcmp(data.Series, m),:));
survey = cell2mat(G(:,2));
survey.year = sortrows(survey)
end

Answers (1)

Stephen23
Stephen23 on 3 Jan 2021
Edited: Stephen23 on 3 Jan 2021
Either
data = sortrows(data,{'Series','Year'})
or
data = sortrows(data,{'Year','Series'})
depending on what you expect the order to be like.

Categories

Find more on Data Import from MATLAB 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!