MATLAB Answers

Append each updated value of the same for loop variable to .mat file

1 view (last 30 days)
Gulce Lale
Gulce Lale on 29 Nov 2020
Commented: Mario Malic on 30 Nov 2020
I want to append same variable with the different updated values to an existing .mat file. When I use -append, it only replaces the existing value, and I can always see the final updated value.
I am trying to save each random_blink value to the random_blink.mat file. Am I missing something or could anyone know how to fix this code ?
for i = 1:video_number
vidName = video_name{i+1};
vidPath = strcat(videosPathToBeEdited, vidName);
V= VideoReader(vidPath);
RGB= readFrame(V);
imshow(RGB);
%%here goes the saving to .mat file code
xmin=0.9;
xmax=1.5;
n=1;
random_blink = xmin+rand(1,n)*(xmax-xmin);
save('random_blink.mat','random_blink','-append','-v7.3');
end

  0 Comments

Sign in to comment.

Answers (1)

Mario Malic
Mario Malic on 29 Nov 2020
Hello,
I wouldn't know how append option works in detail, but It overwrites the variable in your case. Better option is to preallocate a variable, fill the data in the for loop and save it when it ends.
random_blink = zeros(video_number,1);
for i = 1:video_number
vidName = video_name{i+1};
vidPath = strcat(videosPathToBeEdited, vidName);
V= VideoReader(vidPath);
RGB= readFrame(V);
imshow(RGB);
%%here goes the saving to .mat file code
xmin=0.9;
xmax=1.5;
n=1;
random_blink(i) = xmin+rand(1,n)*(xmax-xmin);
end
save('random_blink.mat','random_blink');

  4 Comments

Show 1 older comment
Mario Malic
Mario Malic on 30 Nov 2020
starting_second is a scalar, blink_time is a scalar, and random_blink is a vector with current code. This makes starting_second a vector as well.
starting_second = blink_time{i+1} - random_blink;
% Example
0 - [1;2]
ans =
-1
-2
% Example
0 - [1 2]
ans =
-1 -2
So, you have to change it to
starting_second = blink_time{i+1} - random_blink(i); % or i+1 *
There might be an issue with your code - the i+1 index. It will skip first value in video_name{i+1} and will try to access the index that doesn't exist and cause an error.
Mario Malic
Mario Malic on 30 Nov 2020
Well, I can't help with little information I have from this post, this is not efficient for me. Variable blink_time, is a cell, therefore, an element of it can contain a vector (which I don't see defined in this part) that may cause something like this that could happen, as explained in the comment above.
starting_second(i,1) = blink_time{i+1} - random_blink(i);
An example for the loading, deleting and saving.
clear
a = rand(5);
b = rand(3);
save('file.mat', 'a', 'b');
clear a b % removes a and b from workspace
load workspace.mat % now a and b are in workspace
a(1, :) = zeros([1,5]);
% a(1,:) = []; % will remove the whole row
save ('file.mat', 'a', 'b');
Mario Malic
Mario Malic on 30 Nov 2020
I'd suggest to:
  • Revert back to old code and try to do what you want from there.
  • Fix your variable types (If your variable has only the numeric values, then cell array is not the data type you should use)
  • Consider if your indexing issue - i+1 does what you want to
I'm afraid that I can't help further as the issue is not clear to me and guessing what the code does is not great.
Will reply to this one: 'I just want to store each value of random_blink value when I run the code which will give me 26 values since I have 26 inputs. I need to store the values because I will use them later to calculate another value in another script,..'. If you save a variable a that is an array with 26 rows, you load it and call the function for each element of a.
You should visit MATLAB Onramp to get you through indexing and variable types.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!