MATLAB: I have a .mat file with field names like c0_Curve_Someheadername. When I load the .mat file into Matlab it reorders the files in a differ sequential order (c0_Curve_Header1; c10_Curve_Header10; etc). Is there an eas way to reorganize the field name

fieldname sequencing

I've tried to dynamically call the different fields but I'm not sure how to handle the changing header strings (i.e. c0_Curve_ChangingHeaderStrings).
%%Get info on .mat file and sample names
% Get .mat file name, open and load it
[file, path] = uigetfile('.mat', 'Open PSTrace .mat file');
matObj = matfile([path file]); %'Ugo_February_26th_Data.mat'

load([path file]);
% Get details of .mat file
details = whos(matObj);
% Get number of scanned curves and scans/sample
N = numel(fieldnames(matObj))-1;
Number_of_Samples = input('Number of voltammetry curves per sample = ');
% Get .xls file name, open and load it
[file, path] = uigetfile('.xlsx', 'Open Excel file with sample names');
[num, Sample_Label, raw] = xlsread([path file]); %'Ugo_February_26th_Data.mat'
%%Organizes curves in corret order because of .mat field naming convention
%%Signal Evaluation
Count = 0;
for i = 1:N/Number_of_Samples
for j = 1:Number_of_Samples
%Sample(i,j).s = eval(['c' num2str(Count) '_Curve']); %This works if I have no headers
Sample(i,j).s = eval(details(i).name);
Count = Count +1;
end
end
Any suggestions would be greatly appreciated!

Best Answer

Your code needs a few improvements before getting to the main task:
These are trivially fixed. For example, loading the data like this:
S = load(fullfile(fpth,fnm));
And then it is trivial to get the fieldnames into alphanumeric order: download my FEX submission natsort, which probably gives the order that you want (if not please clearly explain the required order):
C = fieldnames(S);
C = natsort(C);
for k = 1:numel(C)
data = S.(C{k})
...
end
or together with orderfields you could even change the order of the fields in the structure itself:
S = ordefields(S,C);
Notice that it is not actually required to change the order of the fields to access them, once you have cell array C in the correct order. Alternatively you could use the second output of natsort, which is an index of the sort order.
Do you notice how much simpler all of these options are, compared to dynamically accessing variable names?