How do I make a changes to a .mat file with several variables, and then save the .mat file so that is has an idenrtical variable structure?

7 views (last 30 days)
I have a .mat file with several variables. I want to make a change to a single column in one of the variables, and then save that changed .mat file so it has an identical variable structure as the original file. I have loaded the .mat file, successfully changed the column, but am having trouble saving it in the desired format.
First, I will illustrate what happened to explain what I mean. I loaded the .mat file and then applied the multiplier to one of the columns successfully. But when I save the .mat file, I get a different variable structure as shown in my BEFORE and AFTER images below. How do I save the .mat file AFTER making the change to the column so that it has an identical variable structure as the original .mat file (i.e. I want the BEFORE and AFTER variable structures to be the same). Right now the variable structures are different as shown below.
The code I used to load the original mat file, make the change to a single column, and then save the .mat file is shown after these images.
This is the structure of my .mat file at the start when I loaded the original .mat file into MATLAB:
This is the structure of the .mat file after I loaded the file into MATLAB, successfully changed my column, and saved the file (it's different than the BEFORE image, when I want this saved file to have an identical variable structure as the original .mat file shown above):
Here is the code that I used to load the original .mat file (shown in the BEFORE image above), and then make the change, and then save the chnage .mat file (shown in the AFTER image above). How do I save the .mat file with the changes but have the structure of the variables be identical to the original .mat file?
%load the .mat file
folder = 'FilePath';
fullMatFileName = fullfile(folder, FileName')
if ~exist(fullMatFileName, 'file')
message = sprintf('%s does not exist', fullMatFileName);
s = load(fullMatFileName);
%make the desired change to the column
for i=1:length(s.diameter.L)
if find(~any(~isnan(s.diameter.L(:,i))))==1
s.diameter.L(:,i) = NaN;
s.diameter.L(:,i)= 256*sqrt(((s.diameter.L(:,i)))/3.14159)
%save the .mat file that now has the change to that one column
save('FileName', 's')
Thanks so much! I am very new to coding and appreciate the help.

Accepted Answer

Rik on 20 Aug 2021
Use this syntax:

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by