Clear Filters
Clear Filters

Sort Table Variable with Cell Arrays

4 views (last 30 days)
Felipe Dicker
Felipe Dicker on 13 May 2024
Answered: Voss on 13 May 2024
So I have a table being printed out in AppDesigner and I'd like to sort it based on the alphabetical order of the parameters in the first column. I've tried using sortrows but I get the following error:
An error occurred when sorting rows of the variable 't2', whose type is 'cell'.
Caused by:
Cell elements must be character arrays.
Without sorting anything the first few lines of the table so far look like this, with the cells in blue actually being a single column (as if it was merged like in an Excel sheet):
For coding I've done the following:
t1 = {'Log Name';'Parameter';'Distance (m)';'RPM';'Speed (km/h)';'Lat. Accel. (G)'};
t2 = [{' ' id_1 ' '};{'Min Value' 'Max Value' 'Avg/Single Value'};{'-' '-' max(dist_1)};{min(RPM_1) max(RPM_1) mean(RPM_1,'omitnan')};...
{min(v_front_1) max(v_front_1) mean(v_front_1,'omitnan')};{min(ay_1) max(ay_1) mean(ay_1,'omitnan')}];
t = table(t1,t2);
I guess it has something to do with the fact that I'm using cell arrays (which I don't fully understand) to get that "merged cell" look like in Excel but I couldn't find a better way to do it. Thanks in advance.

Answers (1)

Voss
Voss on 13 May 2024
t = table(t1,t2);
[~,idx] = sort(t1(3:end));
t = t([1; 2; 2+idx],:);

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!