Saving a struct to a text file exactly how it appears in command window

36 次查看(过去 30 天)
Hello, I have a struct called settings. I am wanting to save it to a text file in an easily readable format. Im trying to avoid manually using fprintf and have used the table apporach as:
path='F:\settings.txt';
settings = struct; %Create structure called settings
settings.exposure_ms=num2str(app.ExposuremsEditField.Value);
settings.gain=app.GainDropDown.Value;
settings.BlueLaser_mA=num2str(app.mABlueEditField.Value);
settings.GreenLaser_mA=num2str(app.mAGreenEditField.Value);
settings.date= datestr(now,'dd-mmm-yyyy HH-MM-SS'); %get current dateTime
settings
T=struct2table(settings)
class(T)
writetable(T, path,'Delimiter','tab')
However, this gives the field names as "headings" in the table and as they don't align up in a normal text file is quite difficult to read.
I am wanting to get the text file more in the format of the struc itself (i.e. settings), i.e. rows are field names, then on the same row the field value
I have played with other snippeds I've googled but none are letting me do what I want to do.
% M = cell2mat(struct2cell(settings(:)).')
% writematrix(M, path);
or
% T2= rows2vars(T)
% T.Properties
% Tc=table2cell(T)
% Tt = cell2table(Tc','RowNames',T.Properties.VariableNames,'VariableNames',T.Properties.DimensionNames)

采纳的回答

Les Beckham
Les Beckham 2023-5-24
Here is one approach.
path='F:\settings.txt';
settings = struct; %Create structure called settings
% settings.exposure_ms=num2str(app.ExposuremsEditField.Value);
settings.exposure_ms = 200; % << test value
% settings.gain=app.GainDropDown.Value;
settings.gain = 0; % << test value
% settings.BlueLaser_mA=num2str(app.mABlueEditField.Value);
settings.BlueLaser_mA = 300; % << test value
% settings.GreenLaser_mA=num2str(app.mAGreenEditField.Value);
settings.BlueLaser_mA = 290; % << test value
settings.date= datestr(now,'dd-mmm-yyyy HH-MM-SS'); %get current dateTime
settings
settings = struct with fields:
exposure_ms: 200 gain: 0 BlueLaser_mA: 290 date: '24-May-2023 15-38-05'
% T=struct2table(settings)
% class(T)
% writetable(T, path,'Delimiter','tab')
str = formattedDisplayText(settings);
writelines(str, 'settings.txt');
system('cat settings.txt');
exposure_ms: 200 gain: 0 BlueLaser_mA: 290 date: '24-May-2023 15-38-05'
  2 个评论
Jason
Jason 2023-5-24
wow thats awesome - I've not come across formattedDisplayText before,
Also what does the last line with system do?
Les Beckham
Les Beckham 2023-5-24
编辑:Les Beckham 2023-5-24
The system command is just to display the contents of the text file that was written with the writelines command.
Steven Lord's comment above is a good one. Consider changing the name of your structure.
Also (even more importantly), don't name a variable "path" as that is a crucial keyword in Matlab. Call it "filePath" or anything else you want, just not "path".
If this answer solved your issue, please consider accepting it by clicking "Accept this Answer". Thanks.

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by