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

36 ビュー (過去 30 日間)
Jason
Jason 2023 年 5 月 24 日
編集済み: Les Beckham 2023 年 5 月 24 日
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)
  2 件のコメント
Steven Lord
Steven Lord 2023 年 5 月 24 日
I recommend you choose a different name for your struct array as settings is a function in MATLAB.
Jason
Jason 2023 年 5 月 24 日
OK, thanks for passing that info!

サインインしてコメントする。

採用された回答

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 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by