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

9 visualizaciones (últimos 30 días)
Gulce Lale
Gulce Lale el 29 de Nov. de 2020
Comentada: Mario Malic el 30 de Nov. de 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

Respuestas (1)

Mario Malic
Mario Malic el 29 de Nov. de 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 comentarios
Mario Malic
Mario Malic el 30 de Nov. de 2020
Editada: Mario Malic el 30 de Nov. de 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 el 30 de Nov. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Function Creation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by