Saving and loading structural array has different format

1 次查看(过去 30 天)
Hello, I am trying to save simulation results in a stuctural array. at the end of each simmulation, I would like to read the .mat as a stucture add the results, and save the data again. However, when I load the .mat file for the nx1 cell array, it has a diffrent structure from what I saved it as, the resulting structure has a nested stucture. is there any way to load the file so that the structure is the same?
>> results %this would be the sturcute I just loaded
results =
struct with fields:
Ce: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
Ca: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
Cx: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
D: [0 0 0]
ve: [0.1000 0 0]
va: [0.1000 0 0]
mu: [0.1000 0 0]
w: [0 0]
solve_time: 520.6707
>> add = struct('Ce',res_xk(:,1)','Ca',res_xk(:,1)','Cx',res_xk(:,1)','D',res_uk','ve',res_theta(:,1)','va', res_theta(:,2)','mu', res_theta(:,3)','w', vkp','solve_time', solve_time);
results = [results;add];
>> results % add new data to results, the resulting structure is what I want
results =
2×1 struct array with fields:
Ce
Ca
Cx
D
ve
va
mu
w
solve_time
>> save('results.mat', '-struct', 'results') % '-struct' does not work here
Error using save
The argument to -STRUCT must be the name of a scalar structure variable.
>> save('results.mat', 'results') % I think the issue is here, since im saving the struct as a stuct
>> results_loaded = load('results.mat') %this result is not what I want
results_loaded =
struct with fields:
results: [2×1 struct]

采纳的回答

Walter Roberson
Walter Roberson 2020-3-27
results_loaded.results = [results_loaded.results;add];
save('results.mat', '-struct', 'results_loaded');

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by