load mat file without overwriting excluded parameter properties in workspace

2 views (last 30 days)
Working with Matlab R2015b. I have my workspace loaded with parameters, each having multiple different properties (ex. Min, Max, Value). I also have a MAT file that contains a copy of some of these parameters, but only with one property.
In workspace
ex_param =
Value = 5
Min = 0
Max = 10
In MAT file
ex_param
Value: 7
I need to use the MAT file to populate my workspace with new Value properties. If I simply load my MAT file, the entire parameter gets overwritten and I end up with my parameter only having a Value property in my workspace.
In workspace after MAT file load
ex_param
Value: 7
Is there anyway to load the MAT file to only write over the properties included in the MAT file?
What I want in workspace
ex_param =
Value = 7
Min = 0
Max = 10

Answers (1)

dpb
dpb on 24 Mar 2017
No can do if use same variable name; the selection process isn't more granular than the variable in the .mat file, not fields within a structure variable or subsets of arrays or whatever.
You can, however, load the .mat file into a structure which can then retrieve the elements of the save structure which you can use to update the in-memory structure of the same name.
S=load('yourmatfile','ex_param'); % presuming only the one variable is wanted
ex_param.Value=S.ex_param.Value; % update in memory variable from the structure
  3 Comments
dpb
dpb on 24 Mar 2017
Well, you'll have to come up with some other non-redundant storage mechanism, then. If you save the same variable as you have, when you read it you will destroy the in-memory copy; that's "just the way Matlab works".
I can't really answer the looping question; don't have enough info on how these parameters are stored but if they're all named variables like the above then that's got to be awkward no matter what it would seem.
Speed is an issue w/ later release of Matlab; it's the result of the increasing burgeoning of adding new features plus the transition to OOP and complex classes/methods over straightforward parameter access. Compound all the new stuff with the fact that virtually none of the old has been removed and you have "code bloat" to the max... :(
Eventually TMW will probably resolve much of this but I'd expect it to be several years at best and probably will never be as "lean and mean" as were earlier versions. Of course, as a rapid-development environment, while speed is important, it can't be the primary aim or TMW's entire model of increasing functionality fails.
dpb
dpb on 25 Mar 2017
Edited: dpb on 25 Mar 2017
You're looking for Fortran NAMELIST functionality; unfortunately, TMW hasn't implemented such in Matlab. It would seem a similar facility in Matlab would be a reasonable feature/enhancement request. Much of Fortran formatted i/o would be far better than the C-based fscanf-based methods we're stuck with, but that horse has sailed, unfortunately.

Sign in to comment.

Categories

Find more on Software Development Tools in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!