필터 지우기
필터 지우기

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

조회 수: 19 (최근 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)
  댓글 수: 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 CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by