You must first determine the format for your file.
Saving to a MATLAB workspace file (. MAT)
If you donβt need a format and just want to load the data later, use save , for example:
save 'myfile.mat' structarr
Saves an array struct structarr in a binary MAT file named "file.mat". To read it back to the workspace, you can use load :
load 'myfile.mat'
Saving as Comma Separated Values ββ(.CSV)
If you want to save your array of structures in a text file as pairs of values ββseparated by commas, where each pair contains the field name and its value, you can do something in these lines:
%// Extract field data fields = repmat(fieldnames(structarr), numel(structarr), 1); values = struct2cell(structarr); %// Convert all numerical values to strings idx = cellfun(@isnumeric, values); values(idx) = cellfun(@num2str, values(idx), 'UniformOutput', 0); %// Combine field names and values in the same array C = {fields{:}; values{:}}; %// Write fields to CSV file fid = fopen('myfile.csv', 'wt'); fmt_str = repmat('%s,', 1, size(C, 2)); fprintf(fid, [fmt_str(1:end - 1), '\n'], C{:}); fclose(fid);
This solution assumes that each field contains a scalar value or string, but you can expand it as you see fit.
source share