how to write a struct to a .xls or txt file

1 view (last 30 days)
Roger
Roger on 18 Apr 2014
Commented: Image Analyst on 18 Apr 2014
station = struct(...
'name', {'CD2', 'GYA', 'GOM', 'XAN', 'LZH','KMI', 'LSA','MDJ','WMQ','CNS', 'GUL', 'GZH', 'HEF', 'QZH', 'WHN','WZH'},...
'Jingdu',{103.76 ,106.66,103.84,94.81,108.92,102.74,91.10,129.59,87.69,112.59,110.12,113.65,117.17,118.60,114.35,120.40},...
'Weidu',{30.91, 26.46, 36.09, 36.20, 34.03,25.12,29.73,44.62,43.65,28.12,25.12,23.65,31.52,24.94,30.54,27.55});
1.how can i save it into a xls file , and then load it from the xls file?

Answers (1)

Image Analyst
Image Analyst on 18 Apr 2014
Edited: Image Analyst on 18 Apr 2014
Use struct2table() and writetable():
t = struct2table(station)
writetable(t, 'station.xlsx');
  5 Comments
Roger
Roger on 18 Apr 2014
in my code , i need a struct type variable,so i creat a xls file which saves datas i need,but creat a structure in my code , when i need to use it , i just load it ; sometimes data saved in xls file should be changed ,like appending some data, i just changed the xls file . I just want somebody can change data for his needs from xls file and not need to read my code ,or change from code.
Image Analyst
Image Analyst on 18 Apr 2014
OK, so you read it in with some unspecified function (probably xlsread) and then you convert the array or cell array into a structure for convenience elsewhere in your program. OK fine. If you then change that structure and want to save it back out in the original form back to your Excel workbook file, you're going to have to either change your original cell array that you read in, or else use struct2table. Then you're going to have to write back that changed variable to the workbook file with xlswrite() or writetable().

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!