Create .mat file which appends a variable each time a function is executed

18 views (last 30 days)
Hi
I have a matlab code which contains functions (function()) and it runs until stopped. The code watches for files to be added to a directory and then executes the functions that follows. One of the functions does calculations and then has an output of 4 different variables. I want to save these variables in a directory on my PC. So Basically I want to create an empty .mat file, and then as a new file gets added and the script runs, the new values for these 4 variable should be appended to the .mat file in matrix form if possible. I have tried but did not get it right. Can someone please help
Thank you

Accepted Answer

Jeff Miller
Jeff Miller on 15 Aug 2023
Maybe you can use the function 'matfile' to do what you want. Here is a little example:
% This is part of the main program / startup:
global my_file;
my_file = matfile("my_file_name.mat","Writable",true);
my_file.my_matrix = zeros(0,4); % Make a matrix with 4 columns, no rows yet
% This just simulates calling the function a number of times--
% it sounds like you will call it every time a new file appears?
for ii=1:4
my_function;
end
my_file.my_matrix % This line just displays the result of running this script to show what it does
% This is a substitute for your function just to use in this example.
function my_function()
global my_file;
my_results = rand(1,4); % calculate your four values
my_file.my_matrix(end+1,:) = my_results; % this command appends another
% row onto the end of the matrix.
% the values in that row will
% be the four values computed
% this time by the function
end
  1 Comment
Walter Roberson
Walter Roberson on 15 Aug 2023
Note that in order to write data using the matfile() facility, then the .mat file must be a -v7.3 file.
If the file you named (here "my_file_name.mat") does not already exist, then MATLAB will automatically create a -v7.3 file.
However if the file already exists and is not a -v7.3 file then matfile will warn you that it will not be able to write, and then will fail when you request the writing.

Sign in to comment.

More Answers (1)

dpb
dpb on 15 Aug 2023
Moved: dpb on 15 Aug 2023
.mat files aren't sequential files; the "save -append" syntax doesn't add to an existing variable; it adds a new variable to the existing .mat file. If the same variable name is used, then it overwrites the variable of that name such that you'll end up only with the last iteration of your variables being saved. There's no way around this with a .mat file other than the very expensive alternative of reading the existing variables into memory, appending the new values to those and then rewriting.
You need another storage scheme; probably the better choice would be to simply open a file at the beginning of the script/function and then close it when done. It will be more efficient if the variables were collected as an array, but for only four it's not too terrible to code otherwise.
...
permiss=input('New File (N[ew])?','s') % user choice to overwrite/append
if startsWith(permiss,'N','ignorecase',1) % your choice how to decide which
p='w';
else
p='a';
end
fid=fopen(fullfile('foldername','filename.bin'),p); % open a file for output
while(continuesflag) % runs until...
...
[a,b,c,d]=yourfunction(inputs); % do computations
fwrite(fid,[a b c d]) % and output them
...
end
fid=fclose(fid); % close when done
  1 Comment
Stephen23
Stephen23 on 15 Aug 2023
"There's no way around this with a .mat file other than the very expensive alternative of reading the existing variables into memory, appending the new values to those and then rewriting."

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!